1<?xml version="1.0" encoding="iso-8859-1"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4<html> 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-2025 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 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 <title>Expat XML Parser</title> 46 <meta name="author" content="Clark Cooper, coopercc@netheaven.com" /> 47 <meta http-equiv="Content-Style-Type" content="text/css" /> 48 <link href="ok.min.css" rel="stylesheet" type="text/css" /> 49 <link href="style.css" rel="stylesheet" type="text/css" /> 50</head> 51<body> 52 <div> 53 <h1> 54 The Expat XML Parser 55 <small>Release 2.7.3</small> 56 </h1> 57 </div> 58<div class="content"> 59 60<p>Expat is a library, written in C, for parsing XML documents. It's 61the underlying XML parser for the open source Mozilla project, Perl's 62<code>XML::Parser</code>, Python's <code>xml.parsers.expat</code>, and 63other open-source XML parsers.</p> 64 65<p>This library is the creation of James Clark, who's also given us 66groff (an nroff look-alike), Jade (an implementation of ISO's DSSSL 67stylesheet language for SGML), XP (a Java XML parser package), XT (a 68Java XSL engine). James was also the technical lead on the XML 69Working Group at W3C that produced the XML specification.</p> 70 71<p>This is free software, licensed under the <a 72href="../COPYING">MIT/X Consortium license</a>. You may download it 73from <a href="https://libexpat.github.io/">the Expat home page</a>. 74</p> 75 76<p>The bulk of this document was originally commissioned as an article 77by <a href="https://www.xml.com/">XML.com</a>. They graciously allowed 78Clark Cooper to retain copyright and to distribute it with Expat. 79This version has been substantially extended to include documentation 80on features which have been added since the original article was 81published, and additional information on using the original 82interface.</p> 83 84<hr /> 85<h2>Table of Contents</h2> 86<ul> 87 <li><a href="#overview">Overview</a></li> 88 <li><a href="#building">Building and Installing</a></li> 89 <li><a href="#using">Using Expat</a></li> 90 <li><a href="#reference">Reference</a> 91 <ul> 92 <li><a href="#creation">Parser Creation Functions</a> 93 <ul> 94 <li><a href="#XML_ParserCreate">XML_ParserCreate</a></li> 95 <li><a href="#XML_ParserCreateNS">XML_ParserCreateNS</a></li> 96 <li><a href="#XML_ParserCreate_MM">XML_ParserCreate_MM</a></li> 97 <li><a href="#XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</a></li> 98 <li><a href="#XML_ParserFree">XML_ParserFree</a></li> 99 <li><a href="#XML_ParserReset">XML_ParserReset</a></li> 100 </ul> 101 </li> 102 <li><a href="#parsing">Parsing Functions</a> 103 <ul> 104 <li><a href="#XML_Parse">XML_Parse</a></li> 105 <li><a href="#XML_ParseBuffer">XML_ParseBuffer</a></li> 106 <li><a href="#XML_GetBuffer">XML_GetBuffer</a></li> 107 <li><a href="#XML_StopParser">XML_StopParser</a></li> 108 <li><a href="#XML_ResumeParser">XML_ResumeParser</a></li> 109 <li><a href="#XML_GetParsingStatus">XML_GetParsingStatus</a></li> 110 </ul> 111 </li> 112 <li><a href="#setting">Handler Setting Functions</a> 113 <ul> 114 <li><a href="#XML_SetStartElementHandler">XML_SetStartElementHandler</a></li> 115 <li><a href="#XML_SetEndElementHandler">XML_SetEndElementHandler</a></li> 116 <li><a href="#XML_SetElementHandler">XML_SetElementHandler</a></li> 117 <li><a href="#XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</a></li> 118 <li><a href="#XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</a></li> 119 <li><a href="#XML_SetCommentHandler">XML_SetCommentHandler</a></li> 120 <li><a href="#XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</a></li> 121 <li><a href="#XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</a></li> 122 <li><a href="#XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</a></li> 123 <li><a href="#XML_SetDefaultHandler">XML_SetDefaultHandler</a></li> 124 <li><a href="#XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</a></li> 125 <li><a href="#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></li> 126 <li><a href="#XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg</a></li> 127 <li><a href="#XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler</a></li> 128 <li><a href="#XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</a></li> 129 <li><a href="#XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</a></li> 130 <li><a href="#XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</a></li> 131 <li><a href="#XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</a></li> 132 <li><a href="#XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</a></li> 133 <li><a href="#XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</a></li> 134 <li><a href="#XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</a></li> 135 <li><a href="#XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</a></li> 136 <li><a href="#XML_SetElementDeclHandler">XML_SetElementDeclHandler</a></li> 137 <li><a href="#XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</a></li> 138 <li><a href="#XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</a></li> 139 <li><a href="#XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</a></li> 140 <li><a href="#XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</a></li> 141 <li><a href="#XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</a></li> 142 </ul> 143 </li> 144 <li><a href="#position">Parse Position and Error Reporting Functions</a> 145 <ul> 146 <li><a href="#XML_GetErrorCode">XML_GetErrorCode</a></li> 147 <li><a href="#XML_ErrorString">XML_ErrorString</a></li> 148 <li><a href="#XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</a></li> 149 <li><a href="#XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</a></li> 150 <li><a href="#XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</a></li> 151 <li><a href="#XML_GetCurrentByteCount">XML_GetCurrentByteCount</a></li> 152 <li><a href="#XML_GetInputContext">XML_GetInputContext</a></li> 153 </ul> 154 </li> 155 <li> 156 <a href="#attack-protection">Attack Protection</a> 157 <ul> 158 <li><a href="#XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</a></li> 159 <li><a href="#XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</a></li> 160 <li><a href="#XML_SetAllocTrackerMaximumAmplification">XML_SetAllocTrackerMaximumAmplification</a></li> 161 <li><a href="#XML_SetAllocTrackerActivationThreshold">XML_SetAllocTrackerActivationThreshold</a></li> 162 <li><a href="#XML_SetReparseDeferralEnabled">XML_SetReparseDeferralEnabled</a></li> 163 </ul> 164 </li> 165 <li><a href="#miscellaneous">Miscellaneous Functions</a> 166 <ul> 167 <li><a href="#XML_SetUserData">XML_SetUserData</a></li> 168 <li><a href="#XML_GetUserData">XML_GetUserData</a></li> 169 <li><a href="#XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</a></li> 170 <li><a href="#XML_SetBase">XML_SetBase</a></li> 171 <li><a href="#XML_GetBase">XML_GetBase</a></li> 172 <li><a href="#XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</a></li> 173 <li><a href="#XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</a></li> 174 <li><a href="#XML_GetAttributeInfo">XML_GetAttributeInfo</a></li> 175 <li><a href="#XML_SetEncoding">XML_SetEncoding</a></li> 176 <li><a href="#XML_SetParamEntityParsing">XML_SetParamEntityParsing</a></li> 177 <li><a href="#XML_SetHashSalt">XML_SetHashSalt</a></li> 178 <li><a href="#XML_UseForeignDTD">XML_UseForeignDTD</a></li> 179 <li><a href="#XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</a></li> 180 <li><a href="#XML_DefaultCurrent">XML_DefaultCurrent</a></li> 181 <li><a href="#XML_ExpatVersion">XML_ExpatVersion</a></li> 182 <li><a href="#XML_ExpatVersionInfo">XML_ExpatVersionInfo</a></li> 183 <li><a href="#XML_GetFeatureList">XML_GetFeatureList</a></li> 184 <li><a href="#XML_FreeContentModel">XML_FreeContentModel</a></li> 185 <li><a href="#XML_MemMalloc">XML_MemMalloc</a></li> 186 <li><a href="#XML_MemRealloc">XML_MemRealloc</a></li> 187 <li><a href="#XML_MemFree">XML_MemFree</a></li> 188 </ul> 189 </li> 190 </ul> 191 </li> 192</ul> 193 194<hr /> 195<h2><a name="overview">Overview</a></h2> 196 197<p>Expat is a stream-oriented parser. You register callback (or 198handler) functions with the parser and then start feeding it the 199document. As the parser recognizes parts of the document, it will 200call the appropriate handler for that part (if you've registered one.) 201The document is fed to the parser in pieces, so you can start parsing 202before you have all the document. This also allows you to parse really 203huge documents that won't fit into memory.</p> 204 205<p>Expat can be intimidating due to the many kinds of handlers and 206options you can set. But you only need to learn four functions in 207order to do 90% of what you'll want to do with it:</p> 208 209<dl> 210 211<dt><code><a href= "#XML_ParserCreate" 212 >XML_ParserCreate</a></code></dt> 213 <dd>Create a new parser object.</dd> 214 215<dt><code><a href= "#XML_SetElementHandler" 216 >XML_SetElementHandler</a></code></dt> 217 <dd>Set handlers for start and end tags.</dd> 218 219<dt><code><a href= "#XML_SetCharacterDataHandler" 220 >XML_SetCharacterDataHandler</a></code></dt> 221 <dd>Set handler for text.</dd> 222 223<dt><code><a href= "#XML_Parse" 224 >XML_Parse</a></code></dt> 225 <dd>Pass a buffer full of document to the parser</dd> 226</dl> 227 228<p>These functions and others are described in the <a 229href="#reference">reference</a> part of this document. The reference 230section also describes in detail the parameters passed to the 231different types of handlers.</p> 232 233<p>Let's look at a very simple example program that only uses 3 of the 234above functions (it doesn't need to set a character handler.) The 235program <a href="../examples/outline.c">outline.c</a> prints an 236element outline, indenting child elements to distinguish them from the 237parent element that contains them. The start handler does all the 238work. It prints two indenting spaces for every level of ancestor 239elements, then it prints the element and attribute 240information. Finally it increments the global <code>Depth</code> 241variable.</p> 242 243<pre class="eg"> 244int Depth; 245 246void XMLCALL 247start(void *data, const char *el, const char **attr) { 248 int i; 249 250 for (i = 0; i < Depth; i++) 251 printf(" "); 252 253 printf("%s", el); 254 255 for (i = 0; attr[i]; i += 2) { 256 printf(" %s='%s'", attr[i], attr[i + 1]); 257 } 258 259 printf("\n"); 260 Depth++; 261} /* End of start handler */ 262</pre> 263 264<p>The end tag simply does the bookkeeping work of decrementing 265<code>Depth</code>.</p> 266<pre class="eg"> 267void XMLCALL 268end(void *data, const char *el) { 269 Depth--; 270} /* End of end handler */ 271</pre> 272 273<p>Note the <code>XMLCALL</code> annotation used for the callbacks. 274This is used to ensure that the Expat and the callbacks are using the 275same calling convention in case the compiler options used for Expat 276itself and the client code are different. Expat tries not to care 277what the default calling convention is, though it may require that it 278be compiled with a default convention of "cdecl" on some platforms. 279For code which uses Expat, however, the calling convention is 280specified by the <code>XMLCALL</code> annotation on most platforms; 281callbacks should be defined using this annotation.</p> 282 283<p>The <code>XMLCALL</code> annotation was added in Expat 1.95.7, but 284existing working Expat applications don't need to add it (since they 285are already using the "cdecl" calling convention, or they wouldn't be 286working). The annotation is only needed if the default calling 287convention may be something other than "cdecl". To use the annotation 288safely with older versions of Expat, you can conditionally define it 289<em>after</em> including Expat's header file:</p> 290 291<pre class="eg"> 292#include <expat.h> 293 294#ifndef XMLCALL 295#if defined(_MSC_EXTENSIONS) && !defined(__BEOS__) && !defined(__CYGWIN__) 296#define XMLCALL __cdecl 297#elif defined(__GNUC__) 298#define XMLCALL __attribute__((cdecl)) 299#else 300#define XMLCALL 301#endif 302#endif 303</pre> 304 305<p>After creating the parser, the main program just has the job of 306shoveling the document to the parser so that it can do its work.</p> 307 308<hr /> 309<h2><a name="building">Building and Installing Expat</a></h2> 310 311<p>The Expat distribution comes as a compressed (with GNU gzip) tar 312file. You may download the latest version from <a href= 313"https://sourceforge.net/projects/expat/" >Source Forge</a>. After 314unpacking this, cd into the directory. Then follow either the Win32 315directions or Unix directions below.</p> 316 317<h3>Building under Win32</h3> 318 319<p>If you're using the GNU compiler under cygwin, follow the Unix 320directions in the next section. Otherwise if you have Microsoft's 321Developer Studio installed, 322you can use CMake to generate a <code>.sln</code> file, e.g. 323<code> 324cmake -G"Visual Studio 17 2022" -DCMAKE_BUILD_TYPE=RelWithDebInfo . 325</code>, and build Expat using <code>msbuild /m expat.sln</code> after.</p> 326 327<p>Alternatively, you may download the Win32 binary package that 328contains the "expat.h" include file and a pre-built DLL.</p> 329 330<h3>Building under Unix (or GNU)</h3> 331 332<p>First you'll need to run the configure shell script in order to 333configure the Makefiles and headers for your system.</p> 334 335<p>If you're happy with all the defaults that configure picks for you, 336and you have permission on your system to install into /usr/local, you 337can install Expat with this sequence of commands:</p> 338 339<pre class="eg"> 340./configure 341make 342make install 343</pre> 344 345<p>There are some options that you can provide to this script, but the 346only one we'll mention here is the <code>--prefix</code> option. You 347can find out all the options available by running configure with just 348the <code>--help</code> option.</p> 349 350<p>By default, the configure script sets things up so that the library 351gets installed in <code>/usr/local/lib</code> and the associated 352header file in <code>/usr/local/include</code>. But if you were to 353give the option, <code>--prefix=/home/me/mystuff</code>, then the 354library and header would get installed in 355<code>/home/me/mystuff/lib</code> and 356<code>/home/me/mystuff/include</code> respectively.</p> 357 358<h3>Configuring Expat Using the Pre-Processor</h3> 359 360<p>Expat's feature set can be configured using a small number of 361pre-processor definitions. The symbols are:</p> 362 363<dl class="cpp-symbols"> 364<dt><a name="XML_GE">XML_GE</a></dt> 365<dd> 366Added in Expat 2.6.0. 367Include support for 368<a href="https://www.w3.org/TR/2006/REC-xml-20060816/#sec-physical-struct">general entities</a> 369(syntax <code>&e1;</code> to reference and 370syntax <code><!ENTITY e1 'value1'></code> (an internal general entity) or 371<code><!ENTITY e2 SYSTEM 'file2'></code> (an external general entity) to declare). 372With <code>XML_GE</code> enabled, general entities will be replaced by their declared replacement text; 373for this to work for <em>external</em> general entities, in addition an 374<code><a href="#XML_SetExternalEntityRefHandler">XML_ExternalEntityRefHandler</a></code> must be set using 375<code><a href="#XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</a></code>. 376Also, enabling <code>XML_GE</code> makes 377the functions <code><a href="#XML_SetBillionLaughsAttackProtectionMaximumAmplification"> 378XML_SetBillionLaughsAttackProtectionMaximumAmplification</a></code> and <code> 379<a href="#XML_SetBillionLaughsAttackProtectionActivationThreshold"> 380XML_SetBillionLaughsAttackProtectionActivationThreshold</a></code> available. 381<br/> 382With <code>XML_GE</code> disabled, Expat has a smaller memory footprint and can be faster, but will 383not load external general entities and will replace all general entities 384(except the <a href="https://www.w3.org/TR/2006/REC-xml-20060816/#sec-predefined-ent">predefined five</a>: 385<code>amp</code>, <code>apos</code>, <code>gt</code>, <code>lt</code>, <code>quot</code>) 386with a self-reference: 387for example, referencing an entity <code>e1</code> via <code>&e1;</code> will be replaced 388by text <code>&e1;</code>. 389</dd> 390 391<dt><a name="XML_DTD">XML_DTD</a></dt> 392<dd>Include support for using and reporting DTD-based content. If 393this is defined, default attribute values from an external DTD subset 394are reported and attribute value normalization occurs based on the 395type of attributes defined in the external subset. Without 396this, Expat has a smaller memory footprint and can be faster, but will 397not load external parameter entities or process conditional sections. If defined, makes 398the functions <code><a 399href="#XML_SetBillionLaughsAttackProtectionMaximumAmplification"> 400XML_SetBillionLaughsAttackProtectionMaximumAmplification</a></code> and <code> 401<a href="#XML_SetBillionLaughsAttackProtectionActivationThreshold"> 402XML_SetBillionLaughsAttackProtectionActivationThreshold</a></code> available.</dd> 403 404<dt><a name="XML_NS">XML_NS</a></dt> 405<dd>When defined, support for the <cite><a href= 406"https://www.w3.org/TR/REC-xml-names/" >Namespaces in XML</a></cite> 407specification is included.</dd> 408 409<dt><a name="XML_UNICODE">XML_UNICODE</a></dt> 410<dd>When defined, character data reported to the application is 411encoded in UTF-16 using wide characters of the type 412<code>XML_Char</code>. This is implied if 413<code>XML_UNICODE_WCHAR_T</code> is defined.</dd> 414 415<dt><a name="XML_UNICODE_WCHAR_T">XML_UNICODE_WCHAR_T</a></dt> 416<dd>If defined, causes the <code>XML_Char</code> character type to be 417defined using the <code>wchar_t</code> type; otherwise, <code>unsigned 418short</code> is used. Defining this implies 419<code>XML_UNICODE</code>.</dd> 420 421<dt><a name="XML_LARGE_SIZE">XML_LARGE_SIZE</a></dt> 422<dd>If defined, causes the <code>XML_Size</code> and <code>XML_Index</code> 423integer types to be at least 64 bits in size. This is intended to support 424processing of very large input streams, where the return values of 425<code><a href="#XML_GetCurrentByteIndex" >XML_GetCurrentByteIndex</a></code>, 426<code><a href="#XML_GetCurrentLineNumber" >XML_GetCurrentLineNumber</a></code> and 427<code><a href="#XML_GetCurrentColumnNumber" >XML_GetCurrentColumnNumber</a></code> 428could overflow. It may not be supported by all compilers, and is turned 429off by default.</dd> 430 431<dt><a name="XML_CONTEXT_BYTES">XML_CONTEXT_BYTES</a></dt> 432<dd>The number of input bytes of markup context which the parser will 433ensure are available for reporting via <code><a href= 434"#XML_GetInputContext" >XML_GetInputContext</a></code>. This is 435normally set to 1024, and must be set to a positive integer to enable. 436If this is set to zero, the input context will not be available and <code><a 437href= "#XML_GetInputContext" >XML_GetInputContext</a></code> will 438always report <code>NULL</code>. Without this, Expat has a smaller memory 439footprint and can be faster.</dd> 440 441<dt><a name="XML_STATIC">XML_STATIC</a></dt> 442<dd>On Windows, this should be set if Expat is going to be linked 443statically with the code that calls it; this is required to get all 444the right MSVC magic annotations correct. This is ignored on other 445platforms.</dd> 446 447<dt><a name="XML_ATTR_INFO">XML_ATTR_INFO</a></dt> 448<dd>If defined, makes the additional function <code><a href= 449"#XML_GetAttributeInfo" >XML_GetAttributeInfo</a></code> available 450for reporting attribute byte offsets.</dd> 451</dl> 452 453<hr /> 454<h2><a name="using">Using Expat</a></h2> 455 456<h3>Compiling and Linking Against Expat</h3> 457 458<p>Unless you installed Expat in a location not expected by your 459compiler and linker, all you have to do to use Expat in your programs 460is to include the Expat header (<code>#include <expat.h></code>) 461in your files that make calls to it and to tell the linker that it 462needs to link against the Expat library. On Unix systems, this would 463usually be done with the <code>-lexpat</code> argument. Otherwise, 464you'll need to tell the compiler where to look for the Expat header 465and the linker where to find the Expat library. You may also need to 466take steps to tell the operating system where to find this library at 467run time.</p> 468 469<p>On a Unix-based system, here's what a Makefile might look like when 470Expat is installed in a standard location:</p> 471 472<pre class="eg"> 473CC=cc 474LDFLAGS= 475LIBS= -lexpat 476xmlapp: xmlapp.o 477 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS) 478</pre> 479 480<p>If you installed Expat in, say, <code>/home/me/mystuff</code>, then 481the Makefile would look like this:</p> 482 483<pre class="eg"> 484CC=cc 485CFLAGS= -I/home/me/mystuff/include 486LDFLAGS= 487LIBS= -L/home/me/mystuff/lib -lexpat 488xmlapp: xmlapp.o 489 $(CC) $(LDFLAGS) -o xmlapp xmlapp.o $(LIBS) 490</pre> 491 492<p>You'd also have to set the environment variable 493<code>LD_LIBRARY_PATH</code> to <code>/home/me/mystuff/lib</code> (or 494to <code>${LD_LIBRARY_PATH}:/home/me/mystuff/lib</code> if 495LD_LIBRARY_PATH already has some directories in it) in order to run 496your application.</p> 497 498<h3>Expat Basics</h3> 499 500<p>As we saw in the example in the overview, the first step in parsing 501an XML document with Expat is to create a parser object. There are <a 502href="#creation">three functions</a> in the Expat API for creating a 503parser object. However, only two of these (<code><a href= 504"#XML_ParserCreate" >XML_ParserCreate</a></code> and <code><a href= 505"#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>) can be used for 506constructing a parser for a top-level document. The object returned 507by these functions is an opaque pointer (i.e. "expat.h" declares it as 508void *) to data with further internal structure. In order to free the 509memory associated with this object you must call <code><a href= 510"#XML_ParserFree" >XML_ParserFree</a></code>. Note that if you have 511provided any <a href="#userdata">user data</a> that gets stored in the 512parser, then your application is responsible for freeing it prior to 513calling <code>XML_ParserFree</code>.</p> 514 515<p>The objects returned by the parser creation functions are good for 516parsing only one XML document or external parsed entity. If your 517application needs to parse many XML documents, then it needs to create 518a parser object for each one. The best way to deal with this is to 519create a higher level object that contains all the default 520initialization you want for your parser objects.</p> 521 522<p>Walking through a document hierarchy with a stream oriented parser 523will require a good stack mechanism in order to keep track of current 524context. For instance, to answer the simple question, "What element 525does this text belong to?" requires a stack, since the parser may have 526descended into other elements that are children of the current one and 527has encountered this text on the way out.</p> 528 529<p>The things you're likely to want to keep on a stack are the 530currently opened element and it's attributes. You push this 531information onto the stack in the start handler and you pop it off in 532the end handler.</p> 533 534<p>For some tasks, it is sufficient to just keep information on what 535the depth of the stack is (or would be if you had one.) The outline 536program shown above presents one example. Another such task would be 537skipping over a complete element. When you see the start tag for the 538element you want to skip, you set a skip flag and record the depth at 539which the element started. When the end tag handler encounters the 540same depth, the skipped element has ended and the flag may be 541cleared. If you follow the convention that the root element starts at 5421, then you can use the same variable for skip flag and skip 543depth.</p> 544 545<pre class="eg"> 546void 547init_info(Parseinfo *info) { 548 info->skip = 0; 549 info->depth = 1; 550 /* Other initializations here */ 551} /* End of init_info */ 552 553void XMLCALL 554rawstart(void *data, const char *el, const char **attr) { 555 Parseinfo *inf = (Parseinfo *) data; 556 557 if (! inf->skip) { 558 if (should_skip(inf, el, attr)) { 559 inf->skip = inf->depth; 560 } 561 else 562 start(inf, el, attr); /* This does rest of start handling */ 563 } 564 565 inf->depth++; 566} /* End of rawstart */ 567 568void XMLCALL 569rawend(void *data, const char *el) { 570 Parseinfo *inf = (Parseinfo *) data; 571 572 inf->depth--; 573 574 if (! inf->skip) 575 end(inf, el); /* This does rest of end handling */ 576 577 if (inf->skip == inf->depth) 578 inf->skip = 0; 579} /* End rawend */ 580</pre> 581 582<p>Notice in the above example the difference in how depth is 583manipulated in the start and end handlers. The end tag handler should 584be the mirror image of the start tag handler. This is necessary to 585properly model containment. Since, in the start tag handler, we 586incremented depth <em>after</em> the main body of start tag code, then 587in the end handler, we need to manipulate it <em>before</em> the main 588body. If we'd decided to increment it first thing in the start 589handler, then we'd have had to decrement it last thing in the end 590handler.</p> 591 592<h3 id="userdata">Communicating between handlers</h3> 593 594<p>In order to be able to pass information between different handlers 595without using globals, you'll need to define a data structure to hold 596the shared variables. You can then tell Expat (with the <code><a href= 597"#XML_SetUserData" >XML_SetUserData</a></code> function) to pass a 598pointer to this structure to the handlers. This is the first 599argument received by most handlers. In the <a href="#reference" 600>reference section</a>, an argument to a callback function is named 601<code>userData</code> and have type <code>void *</code> if the user 602data is passed; it will have the type <code>XML_Parser</code> if the 603parser itself is passed. When the parser is passed, the user data may 604be retrieved using <code><a href="#XML_GetUserData" 605>XML_GetUserData</a></code>.</p> 606 607<p>One common case where multiple calls to a single handler may need 608to communicate using an application data structure is the case when 609content passed to the character data handler (set by <code><a href= 610"#XML_SetCharacterDataHandler" 611>XML_SetCharacterDataHandler</a></code>) needs to be accumulated. A 612common first-time mistake with any of the event-oriented interfaces to 613an XML parser is to expect all the text contained in an element to be 614reported by a single call to the character data handler. Expat, like 615many other XML parsers, reports such data as a sequence of calls; 616there's no way to know when the end of the sequence is reached until a 617different callback is made. A buffer referenced by the user data 618structure proves both an effective and convenient place to accumulate 619character data.</p> 620 621<!-- XXX example needed here --> 622 623 624<h3>XML Version</h3> 625 626<p>Expat is an XML 1.0 parser, and as such never complains based on 627the value of the <code>version</code> pseudo-attribute in the XML 628declaration, if present.</p> 629 630<p>If an application needs to check the version number (to support 631alternate processing), it should use the <code><a href= 632"#XML_SetXmlDeclHandler" >XML_SetXmlDeclHandler</a></code> function to 633set a handler that uses the information in the XML declaration to 634determine what to do. This example shows how to check that only a 635version number of <code>"1.0"</code> is accepted:</p> 636 637<pre class="eg"> 638static int wrong_version; 639static XML_Parser parser; 640 641static void XMLCALL 642xmldecl_handler(void *userData, 643 const XML_Char *version, 644 const XML_Char *encoding, 645 int standalone) 646{ 647 static const XML_Char Version_1_0[] = {'1', '.', '0', 0}; 648 649 int i; 650 651 for (i = 0; i < (sizeof(Version_1_0) / sizeof(Version_1_0[0])); ++i) { 652 if (version[i] != Version_1_0[i]) { 653 wrong_version = 1; 654 /* also clear all other handlers: */ 655 XML_SetCharacterDataHandler(parser, NULL); 656 ... 657 return; 658 } 659 } 660 ... 661} 662</pre> 663 664<h3>Namespace Processing</h3> 665 666<p>When the parser is created using the <code><a href= 667"#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>, function, Expat 668performs namespace processing. Under namespace processing, Expat 669consumes <code>xmlns</code> and <code>xmlns:...</code> attributes, 670which declare namespaces for the scope of the element in which they 671occur. This means that your start handler will not see these 672attributes. Your application can still be informed of these 673declarations by setting namespace declaration handlers with <a href= 674"#XML_SetNamespaceDeclHandler" 675><code>XML_SetNamespaceDeclHandler</code></a>.</p> 676 677<p>Element type and attribute names that belong to a given namespace 678are passed to the appropriate handler in expanded form. By default 679this expanded form is a concatenation of the namespace URI, the 680separator character (which is the 2nd argument to <code><a href= 681"#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>), and the local 682name (i.e. the part after the colon). Names with undeclared prefixes 683are not well-formed when namespace processing is enabled, and will 684trigger an error. Unprefixed attribute names are never expanded, 685and unprefixed element names are only expanded when they are in the 686scope of a default namespace.</p> 687 688<p>However if <code><a href= "#XML_SetReturnNSTriplet" 689>XML_SetReturnNSTriplet</a></code> has been called with a non-zero 690<code>do_nst</code> parameter, then the expanded form for names with 691an explicit prefix is a concatenation of: URI, separator, local name, 692separator, prefix.</p> 693 694<p>You can set handlers for the start of a namespace declaration and 695for the end of a scope of a declaration with the <code><a href= 696"#XML_SetNamespaceDeclHandler" >XML_SetNamespaceDeclHandler</a></code> 697function. The StartNamespaceDeclHandler is called prior to the start 698tag handler and the EndNamespaceDeclHandler is called after the 699corresponding end tag that ends the namespace's scope. The namespace 700start handler gets passed the prefix and URI for the namespace. For a 701default namespace declaration (xmlns='...'), the prefix will be 702<code>NULL</code>. 703The URI will be <code>NULL</code> for the case where the default namespace is being 704unset. The namespace end handler just gets the prefix for the closing 705scope.</p> 706 707<p>These handlers are called for each declaration. So if, for 708instance, a start tag had three namespace declarations, then the 709StartNamespaceDeclHandler would be called three times before the start 710tag handler is called, once for each declaration.</p> 711 712<h3>Character Encodings</h3> 713 714<p>While XML is based on Unicode, and every XML processor is required 715to recognized UTF-8 and UTF-16 (1 and 2 byte encodings of Unicode), 716other encodings may be declared in XML documents or entities. For the 717main document, an XML declaration may contain an encoding 718declaration:</p> 719<pre> 720<?xml version="1.0" encoding="ISO-8859-2"?> 721</pre> 722 723<p>External parsed entities may begin with a text declaration, which 724looks like an XML declaration with just an encoding declaration:</p> 725<pre> 726<?xml encoding="Big5"?> 727</pre> 728 729<p>With Expat, you may also specify an encoding at the time of 730creating a parser. This is useful when the encoding information may 731come from a source outside the document itself (like a higher level 732protocol.)</p> 733 734<p><a name="builtin_encodings"></a>There are four built-in encodings 735in Expat:</p> 736<ul> 737<li>UTF-8</li> 738<li>UTF-16</li> 739<li>ISO-8859-1</li> 740<li>US-ASCII</li> 741</ul> 742 743<p>Anything else discovered in an encoding declaration or in the 744protocol encoding specified in the parser constructor, triggers a call 745to the <code>UnknownEncodingHandler</code>. This handler gets passed 746the encoding name and a pointer to an <code>XML_Encoding</code> data 747structure. Your handler must fill in this structure and return 748<code>XML_STATUS_OK</code> if it knows how to deal with the 749encoding. Otherwise the handler should return 750<code>XML_STATUS_ERROR</code>. The handler also gets passed a pointer 751to an optional application data structure that you may indicate when 752you set the handler.</p> 753 754<p>Expat places restrictions on character encodings that it can 755support by filling in the <code>XML_Encoding</code> structure. 756include file:</p> 757<ol> 758<li>Every ASCII character that can appear in a well-formed XML document 759must be represented by a single byte, and that byte must correspond to 760it's ASCII encoding (except for the characters $@\^'{}~)</li> 761<li>Characters must be encoded in 4 bytes or less.</li> 762<li>All characters encoded must have Unicode scalar values less than or 763equal to 65535 (0xFFFF)<em>This does not apply to the built-in support 764for UTF-16 and UTF-8</em></li> 765<li>No character may be encoded by more that one distinct sequence of 766bytes</li> 767</ol> 768 769<p><code>XML_Encoding</code> contains an array of integers that 770correspond to the 1st byte of an encoding sequence. If the value in 771the array for a byte is zero or positive, then the byte is a single 772byte encoding that encodes the Unicode scalar value contained in the 773array. A -1 in this array indicates a malformed byte. If the value is 774-2, -3, or -4, then the byte is the beginning of a 2, 3, or 4 byte 775sequence respectively. Multi-byte sequences are sent to the convert 776function pointed at in the <code>XML_Encoding</code> structure. This 777function should return the Unicode scalar value for the sequence or -1 778if the sequence is malformed.</p> 779 780<p>One pitfall that novice Expat users are likely to fall into is that 781although Expat may accept input in various encodings, the strings that 782it passes to the handlers are always encoded in UTF-8 or UTF-16 783(depending on how Expat was compiled). Your application is responsible 784for any translation of these strings into other encodings.</p> 785 786<h3>Handling External Entity References</h3> 787 788<p>Expat does not read or parse external entities directly. Note that 789any external DTD is a special case of an external entity. If you've 790set no <code>ExternalEntityRefHandler</code>, then external entity 791references are silently ignored. Otherwise, it calls your handler with 792the information needed to read and parse the external entity.</p> 793 794<p>Your handler isn't actually responsible for parsing the entity, but 795it is responsible for creating a subsidiary parser with <code><a href= 796"#XML_ExternalEntityParserCreate" 797>XML_ExternalEntityParserCreate</a></code> that will do the job. This 798returns an instance of <code>XML_Parser</code> that has handlers and 799other data structures initialized from the parent parser. You may then 800use <code><a href= "#XML_Parse" >XML_Parse</a></code> or <code><a 801href= "#XML_ParseBuffer">XML_ParseBuffer</a></code> calls against this 802parser. Since external entities my refer to other external entities, 803your handler should be prepared to be called recursively.</p> 804 805<h3>Parsing DTDs</h3> 806 807<p>In order to parse parameter entities, before starting the parse, 808you must call <code><a href= "#XML_SetParamEntityParsing" 809>XML_SetParamEntityParsing</a></code> with one of the following 810arguments:</p> 811<dl> 812<dt><code>XML_PARAM_ENTITY_PARSING_NEVER</code></dt> 813<dd>Don't parse parameter entities or the external subset</dd> 814<dt><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code></dt> 815<dd>Parse parameter entities and the external subset unless 816<code>standalone</code> was set to "yes" in the XML declaration.</dd> 817<dt><code>XML_PARAM_ENTITY_PARSING_ALWAYS</code></dt> 818<dd>Always parse parameter entities and the external subset</dd> 819</dl> 820 821<p>In order to read an external DTD, you also have to set an external 822entity reference handler as described above.</p> 823 824<h3 id="stop-resume">Temporarily Stopping Parsing</h3> 825 826<p>Expat 1.95.8 introduces a new feature: its now possible to stop 827parsing temporarily from within a handler function, even if more data 828has already been passed into the parser. Applications for this 829include</p> 830 831<ul> 832 <li>Supporting the <a href= "https://www.w3.org/TR/xinclude/" 833 >XInclude</a> specification.</li> 834 835 <li>Delaying further processing until additional information is 836 available from some other source.</li> 837 838 <li>Adjusting processor load as task priorities shift within an 839 application.</li> 840 841 <li>Stopping parsing completely (simply free or reset the parser 842 instead of resuming in the outer parsing loop). This can be useful 843 if an application-domain error is found in the XML being parsed or if 844 the result of the parse is determined not to be useful after 845 all.</li> 846</ul> 847 848<p>To take advantage of this feature, the main parsing loop of an 849application needs to support this specifically. It cannot be 850supported with a parsing loop compatible with Expat 1.95.7 or 851earlier (though existing loops will continue to work without 852supporting the stop/resume feature).</p> 853 854<p>An application that uses this feature for a single parser will have 855the rough structure (in pseudo-code):</p> 856 857<pre class="pseudocode"> 858fd = open_input() 859p = create_parser() 860 861if parse_xml(p, fd) { 862 /* suspended */ 863 864 int suspended = 1; 865 866 while (suspended) { 867 do_something_else() 868 if ready_to_resume() { 869 suspended = continue_parsing(p, fd); 870 } 871 } 872} 873</pre> 874 875<p>An application that may resume any of several parsers based on 876input (either from the XML being parsed or some other source) will 877certainly have more interesting control structures.</p> 878 879<p>This C function could be used for the <code>parse_xml</code> 880function mentioned in the pseudo-code above:</p> 881 882<pre class="eg"> 883#define BUFF_SIZE 10240 884 885/* Parse a document from the open file descriptor 'fd' until the parse 886 is complete (the document has been completely parsed, or there's 887 been an error), or the parse is stopped. Return non-zero when 888 the parse is merely suspended. 889*/ 890int 891parse_xml(XML_Parser p, int fd) 892{ 893 for (;;) { 894 int last_chunk; 895 int bytes_read; 896 enum XML_Status status; 897 898 void *buff = XML_GetBuffer(p, BUFF_SIZE); 899 if (buff == NULL) { 900 /* handle error... */ 901 return 0; 902 } 903 bytes_read = read(fd, buff, BUFF_SIZE); 904 if (bytes_read < 0) { 905 /* handle error... */ 906 return 0; 907 } 908 status = XML_ParseBuffer(p, bytes_read, bytes_read == 0); 909 switch (status) { 910 case XML_STATUS_ERROR: 911 /* handle error... */ 912 return 0; 913 case XML_STATUS_SUSPENDED: 914 return 1; 915 } 916 if (bytes_read == 0) 917 return 0; 918 } 919} 920</pre> 921 922<p>The corresponding <code>continue_parsing</code> function is 923somewhat simpler, since it only need deal with the return code from 924<code><a href= "#XML_ResumeParser">XML_ResumeParser</a></code>; it can 925delegate the input handling to the <code>parse_xml</code> 926function:</p> 927 928<pre class="eg"> 929/* Continue parsing a document which had been suspended. The 'p' and 930 'fd' arguments are the same as passed to parse_xml(). Return 931 non-zero when the parse is suspended. 932*/ 933int 934continue_parsing(XML_Parser p, int fd) 935{ 936 enum XML_Status status = XML_ResumeParser(p); 937 switch (status) { 938 case XML_STATUS_ERROR: 939 /* handle error... */ 940 return 0; 941 case XML_ERROR_NOT_SUSPENDED: 942 /* handle error... */ 943 return 0;. 944 case XML_STATUS_SUSPENDED: 945 return 1; 946 } 947 return parse_xml(p, fd); 948} 949</pre> 950 951<p>Now that we've seen what a mess the top-level parsing loop can 952become, what have we gained? Very simply, we can now use the <code><a 953href= "#XML_StopParser" >XML_StopParser</a></code> function to stop 954parsing, without having to go to great lengths to avoid additional 955processing that we're expecting to ignore. As a bonus, we get to stop 956parsing <em>temporarily</em>, and come back to it when we're 957ready.</p> 958 959<p>To stop parsing from a handler function, use the <code><a href= 960"#XML_StopParser" >XML_StopParser</a></code> function. This function 961takes two arguments; the parser being stopped and a flag indicating 962whether the parse can be resumed in the future.</p> 963 964<!-- XXX really need more here --> 965 966 967<hr /> 968<!-- ================================================================ --> 969 970<h2><a name="reference">Expat Reference</a></h2> 971 972<h3><a name="creation">Parser Creation</a></h3> 973 974<h4 id="XML_ParserCreate">XML_ParserCreate</h4> 975<pre class="fcndec"> 976XML_Parser XMLCALL 977XML_ParserCreate(const XML_Char *encoding); 978</pre> 979<div class="fcndef"> 980<p> 981Construct a new parser. If encoding is non-<code>NULL</code>, it specifies a 982character encoding to use for the document. This overrides the document 983encoding declaration. There are four built-in encodings: 984</p> 985<ul> 986<li>US-ASCII</li> 987<li>UTF-8</li> 988<li>UTF-16</li> 989<li>ISO-8859-1</li> 990</ul> 991<p> 992Any other value will invoke a call to the UnknownEncodingHandler. 993</p> 994</div> 995 996<h4 id="XML_ParserCreateNS">XML_ParserCreateNS</h4> 997<pre class="fcndec"> 998XML_Parser XMLCALL 999XML_ParserCreateNS(const XML_Char *encoding, 1000 XML_Char sep); 1001</pre> 1002<div class="fcndef"> 1003Constructs a new parser that has namespace processing in effect. Namespace 1004expanded element names and attribute names are returned as a concatenation 1005of the namespace URI, <em>sep</em>, and the local part of the name. This 1006means that you should pick a character for <em>sep</em> that can't be part 1007of an URI. Since Expat does not check namespace URIs for conformance, the 1008only safe choice for a namespace separator is a character that is illegal 1009in XML. For instance, <code>'\xFF'</code> is not legal in UTF-8, and 1010<code>'\xFFFF'</code> is not legal in UTF-16. There is a special case when 1011<em>sep</em> is the null character <code>'\0'</code>: the namespace URI and 1012the local part will be concatenated without any separator - this is intended 1013to support RDF processors. It is a programming error to use the null separator 1014with <a href= "#XML_SetReturnNSTriplet">namespace triplets</a>.</div> 1015 1016<p><strong>Note:</strong> 1017Expat does not validate namespace URIs (beyond encoding) 1018against RFC 3986 today (and is not required to do so with regard to 1019the XML 1.0 namespaces specification) but it may start doing that 1020in future releases. Before that, an application using Expat must 1021be ready to receive namespace URIs containing non-URI characters. 1022</p> 1023 1024<h4 id="XML_ParserCreate_MM">XML_ParserCreate_MM</h4> 1025<pre class="fcndec"> 1026XML_Parser XMLCALL 1027XML_ParserCreate_MM(const XML_Char *encoding, 1028 const XML_Memory_Handling_Suite *ms, 1029 const XML_Char *sep); 1030</pre> 1031<pre class="signature"> 1032typedef struct { 1033 void *(XMLCALL *malloc_fcn)(size_t size); 1034 void *(XMLCALL *realloc_fcn)(void *ptr, size_t size); 1035 void (XMLCALL *free_fcn)(void *ptr); 1036} XML_Memory_Handling_Suite; 1037</pre> 1038<div class="fcndef"> 1039<p>Construct a new parser using the suite of memory handling functions 1040specified in <code>ms</code>. If <code>ms</code> is <code>NULL</code>, then use the 1041standard set of memory management functions. If <code>sep</code> is 1042non-<code>NULL</code>, then namespace processing is enabled in the created parser 1043and the character pointed at by sep is used as the separator between 1044the namespace URI and the local part of the name.</p> 1045</div> 1046 1047<h4 id="XML_ExternalEntityParserCreate">XML_ExternalEntityParserCreate</h4> 1048<pre class="fcndec"> 1049XML_Parser XMLCALL 1050XML_ExternalEntityParserCreate(XML_Parser p, 1051 const XML_Char *context, 1052 const XML_Char *encoding); 1053</pre> 1054<div class="fcndef"> 1055Construct a new <code>XML_Parser</code> object for parsing an external 1056general entity. Context is the context argument passed in a call to a 1057ExternalEntityRefHandler. Other state information such as handlers, 1058user data, namespace processing is inherited from the parser passed as 1059the 1st argument. So you shouldn't need to call any of the behavior 1060changing functions on this parser (unless you want it to act 1061differently than the parent parser). 1062</div> 1063 1064<h4 id="XML_ParserFree">XML_ParserFree</h4> 1065<pre class="fcndec"> 1066void XMLCALL 1067XML_ParserFree(XML_Parser p); 1068</pre> 1069<div class="fcndef"> 1070Free memory used by the parser. Your application is responsible for 1071freeing any memory associated with <a href="#userdata">user data</a>. 1072</div> 1073 1074<h4 id="XML_ParserReset">XML_ParserReset</h4> 1075<pre class="fcndec"> 1076XML_Bool XMLCALL 1077XML_ParserReset(XML_Parser p, 1078 const XML_Char *encoding); 1079</pre> 1080<div class="fcndef"> 1081Clean up the memory structures maintained by the parser so that it may 1082be used again. After this has been called, <code>parser</code> is 1083ready to start parsing a new document. All handlers are cleared from 1084the parser, except for the unknownEncodingHandler. The parser's external 1085state is re-initialized except for the values of ns and ns_triplets. 1086This function may not be used on a parser created using <code><a href= 1087"#XML_ExternalEntityParserCreate" >XML_ExternalEntityParserCreate</a 1088></code>; it will return <code>XML_FALSE</code> in that case. Returns 1089<code>XML_TRUE</code> on success. Your application is responsible for 1090dealing with any memory associated with <a href="#userdata">user data</a>. 1091</div> 1092 1093<h3><a name="parsing">Parsing</a></h3> 1094 1095<p>To state the obvious: the three parsing functions <code><a href= 1096"#XML_Parse" >XML_Parse</a></code>, <code><a href= "#XML_ParseBuffer"> 1097XML_ParseBuffer</a></code> and <code><a href= "#XML_GetBuffer"> 1098XML_GetBuffer</a></code> must not be called from within a handler 1099unless they operate on a separate parser instance, that is, one that 1100did not call the handler. For example, it is OK to call the parsing 1101functions from within an <code>XML_ExternalEntityRefHandler</code>, 1102if they apply to the parser created by 1103<code><a href= "#XML_ExternalEntityParserCreate" 1104>XML_ExternalEntityParserCreate</a></code>.</p> 1105 1106<p>Note: The <code>len</code> argument passed to these functions 1107should be considerably less than the maximum value for an integer, 1108as it could create an integer overflow situation if the added 1109lengths of a buffer and the unprocessed portion of the previous buffer 1110exceed the maximum integer value. Input data at the end of a buffer 1111will remain unprocessed if it is part of an XML token for which the 1112end is not part of that buffer.</p> 1113 1114<p><a name="isFinal"></a>The application <em>must</em> make a concluding 1115<code><a href="#XML_Parse">XML_Parse</a></code> or 1116<code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> call 1117with <code>isFinal</code> set to <code>XML_TRUE</code>.</p> 1118 1119<h4 id="XML_Parse">XML_Parse</h4> 1120<pre class="fcndec"> 1121enum XML_Status XMLCALL 1122XML_Parse(XML_Parser p, 1123 const char *s, 1124 int len, 1125 int isFinal); 1126</pre> 1127<pre class="signature"> 1128enum XML_Status { 1129 XML_STATUS_ERROR = 0, 1130 XML_STATUS_OK = 1 1131}; 1132</pre> 1133<div class="fcndef"> 1134<p> 1135Parse some more of the document. The string <code>s</code> is a buffer 1136containing part (or perhaps all) of the document. The number of bytes of s 1137that are part of the document is indicated by <code>len</code>. This means 1138that <code>s</code> doesn't have to be null-terminated. It also means that 1139if <code>len</code> is larger than the number of bytes in the block of 1140memory that <code>s</code> points at, then a memory fault is likely. 1141Negative values for <code>len</code> are rejected since Expat 2.2.1. 1142The 1143<code>isFinal</code> parameter informs the parser that this is the last 1144piece of the document. Frequently, the last piece is empty (i.e. 1145<code>len</code> is zero.) 1146</p> 1147 1148<p> 1149If a parse error occurred, it returns <code>XML_STATUS_ERROR</code>. 1150Otherwise it returns <code>XML_STATUS_OK</code> value. 1151Note that regardless of the return value, there is no guarantee that all 1152provided input has been parsed; only after <a href="#isFinal">the 1153concluding call</a> will all handler callbacks and parsing errors have 1154happened. 1155</p> 1156 1157<p> 1158Simplified, <code>XML_Parse</code> can be considered a convenience wrapper 1159that is pairing calls 1160to <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code> 1161and <code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> 1162(when Expat is built with macro <code>XML_CONTEXT_BYTES</code> 1163defined to a positive value, which is both common and default). 1164<code>XML_Parse</code> is then functionally equivalent to calling 1165<code><a href="#XML_GetBuffer">XML_GetBuffer</a></code>, 1166<code>memcpy</code>, and 1167<code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code>. 1168</p> 1169 1170<p> 1171To avoid double copying of the input, direct use of functions 1172<code><a href="#XML_GetBuffer">XML_GetBuffer</a></code> and 1173<code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> is advised 1174for most production use, e.g. 1175if you're using <code>read</code> or similar functionality to fill your 1176buffers, fill directly into the buffer from 1177<code><a href="#XML_GetBuffer">XML_GetBuffer</a></code>, 1178then parse with <code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code>. 1179</p> 1180</div> 1181 1182<h4 id="XML_ParseBuffer">XML_ParseBuffer</h4> 1183<pre class="fcndec"> 1184enum XML_Status XMLCALL 1185XML_ParseBuffer(XML_Parser p, 1186 int len, 1187 int isFinal); 1188</pre> 1189<div class="fcndef"> 1190<p> 1191This is just like <code><a href= "#XML_Parse" >XML_Parse</a></code>, 1192except in this case Expat provides the buffer. By obtaining the 1193buffer from Expat with the <code><a href= "#XML_GetBuffer" 1194>XML_GetBuffer</a></code> function, the application can avoid double 1195copying of the input. 1196</p> 1197 1198<p> 1199Negative values for <code>len</code> are rejected since Expat 2.6.3. 1200</p> 1201</div> 1202 1203<h4 id="XML_GetBuffer">XML_GetBuffer</h4> 1204<pre class="fcndec"> 1205void * XMLCALL 1206XML_GetBuffer(XML_Parser p, 1207 int len); 1208</pre> 1209<div class="fcndef"> 1210Obtain a buffer of size <code>len</code> to read a piece of the document 1211into. A <code>NULL</code> value is returned if Expat can't allocate enough memory for 1212this buffer. A <code>NULL</code> value may also be returned if <code>len</code> is zero. 1213This has to be called prior to every call to 1214<code><a href= "#XML_ParseBuffer" >XML_ParseBuffer</a></code>. A 1215typical use would look like this: 1216 1217<pre class="eg"> 1218for (;;) { 1219 int bytes_read; 1220 void *buff = XML_GetBuffer(p, BUFF_SIZE); 1221 if (buff == NULL) { 1222 /* handle error */ 1223 } 1224 1225 bytes_read = read(docfd, buff, BUFF_SIZE); 1226 if (bytes_read < 0) { 1227 /* handle error */ 1228 } 1229 1230 if (! XML_ParseBuffer(p, bytes_read, bytes_read == 0)) { 1231 /* handle parse error */ 1232 } 1233 1234 if (bytes_read == 0) 1235 break; 1236} 1237</pre> 1238</div> 1239 1240<h4 id="XML_StopParser">XML_StopParser</h4> 1241<pre class="fcndec"> 1242enum XML_Status XMLCALL 1243XML_StopParser(XML_Parser p, 1244 XML_Bool resumable); 1245</pre> 1246<div class="fcndef"> 1247 1248<p>Stops parsing, causing <code><a href= "#XML_Parse" 1249>XML_Parse</a></code> or <code><a href= "#XML_ParseBuffer" 1250>XML_ParseBuffer</a></code> to return. Must be called from within a 1251call-back handler, except when aborting (when <code>resumable</code> 1252is <code>XML_FALSE</code>) an already suspended parser. Some 1253call-backs may still follow because they would otherwise get 1254lost, including</p> 1255<ul> 1256 <li> the end element handler for empty elements when stopped in the 1257 start element handler,</li> 1258 <li> the end namespace declaration handler when stopped in the end 1259 element handler,</li> 1260 <li> the character data handler when stopped in the character data handler 1261 while making multiple call-backs on a contiguous chunk of characters,</li> 1262</ul> 1263<p>and possibly others.</p> 1264 1265<p>This can be called from most handlers, including DTD related 1266call-backs, except when parsing an external parameter entity and 1267<code>resumable</code> is <code>XML_TRUE</code>. Returns 1268<code>XML_STATUS_OK</code> when successful, 1269<code>XML_STATUS_ERROR</code> otherwise. The possible error codes 1270are:</p> 1271<dl> 1272 <dt><code>XML_ERROR_NOT_STARTED</code></dt> 1273 <dd> 1274 when stopping or suspending a parser before it has started, 1275 added in Expat 2.6.4. 1276 </dd> 1277 <dt><code>XML_ERROR_SUSPENDED</code></dt> 1278 <dd>when suspending an already suspended parser.</dd> 1279 <dt><code>XML_ERROR_FINISHED</code></dt> 1280 <dd>when the parser has already finished.</dd> 1281 <dt><code>XML_ERROR_SUSPEND_PE</code></dt> 1282 <dd>when suspending while parsing an external PE.</dd> 1283</dl> 1284 1285<p>Since the stop/resume feature requires application support in the 1286outer parsing loop, it is an error to call this function for a parser 1287not being handled appropriately; see <a href= "#stop-resume" 1288>Temporarily Stopping Parsing</a> for more information.</p> 1289 1290<p>When <code>resumable</code> is <code>XML_TRUE</code> then parsing 1291is <em>suspended</em>, that is, <code><a href= "#XML_Parse" 1292>XML_Parse</a></code> and <code><a href= "#XML_ParseBuffer" 1293>XML_ParseBuffer</a></code> return <code>XML_STATUS_SUSPENDED</code>. 1294Otherwise, parsing is <em>aborted</em>, that is, <code><a href= 1295"#XML_Parse" >XML_Parse</a></code> and <code><a href= 1296"#XML_ParseBuffer" >XML_ParseBuffer</a></code> return 1297<code>XML_STATUS_ERROR</code> with error code 1298<code>XML_ERROR_ABORTED</code>.</p> 1299 1300<p><strong>Note:</strong> 1301This will be applied to the current parser instance only, that is, if 1302there is a parent parser then it will continue parsing when the 1303external entity reference handler returns. It is up to the 1304implementation of that handler to call <code><a href= 1305"#XML_StopParser" >XML_StopParser</a></code> on the parent parser 1306(recursively), if one wants to stop parsing altogether.</p> 1307 1308<p>When suspended, parsing can be resumed by calling <code><a href= 1309"#XML_ResumeParser" >XML_ResumeParser</a></code>.</p> 1310 1311<p>New in Expat 1.95.8.</p> 1312</div> 1313 1314<h4 id="XML_ResumeParser">XML_ResumeParser</h4> 1315<pre class="fcndec"> 1316enum XML_Status XMLCALL 1317XML_ResumeParser(XML_Parser p); 1318</pre> 1319<div class="fcndef"> 1320<p>Resumes parsing after it has been suspended with <code><a href= 1321"#XML_StopParser" >XML_StopParser</a></code>. Must not be called from 1322within a handler call-back. Returns same status codes as <code><a 1323href= "#XML_Parse">XML_Parse</a></code> or <code><a href= 1324"#XML_ParseBuffer" >XML_ParseBuffer</a></code>. An additional error 1325code, <code>XML_ERROR_NOT_SUSPENDED</code>, will be returned if the 1326parser was not currently suspended.</p> 1327 1328<p><strong>Note:</strong> 1329This must be called on the most deeply nested child parser instance 1330first, and on its parent parser only after the child parser has 1331finished, to be applied recursively until the document entity's parser 1332is restarted. That is, the parent parser will not resume by itself 1333and it is up to the application to call <code><a href= 1334"#XML_ResumeParser" >XML_ResumeParser</a></code> on it at the 1335appropriate moment.</p> 1336 1337<p>New in Expat 1.95.8.</p> 1338</div> 1339 1340<h4 id="XML_GetParsingStatus">XML_GetParsingStatus</h4> 1341<pre class="fcndec"> 1342void XMLCALL 1343XML_GetParsingStatus(XML_Parser p, 1344 XML_ParsingStatus *status); 1345</pre> 1346<pre class="signature"> 1347enum XML_Parsing { 1348 XML_INITIALIZED, 1349 XML_PARSING, 1350 XML_FINISHED, 1351 XML_SUSPENDED 1352}; 1353 1354typedef struct { 1355 enum XML_Parsing parsing; 1356 XML_Bool finalBuffer; 1357} XML_ParsingStatus; 1358</pre> 1359<div class="fcndef"> 1360<p>Returns status of parser with respect to being initialized, 1361parsing, finished, or suspended, and whether the final buffer is being 1362processed. The <code>status</code> parameter <em>must not</em> be 1363<code>NULL</code>.</p> 1364 1365<p>New in Expat 1.95.8.</p> 1366</div> 1367 1368 1369<h3><a name="setting">Handler Setting</a></h3> 1370 1371<p>Although handlers are typically set prior to parsing and left alone, an 1372application may choose to set or change the handler for a parsing event 1373while the parse is in progress. For instance, your application may choose 1374to ignore all text not descended from a <code>para</code> element. One 1375way it could do this is to set the character handler when a para start tag 1376is seen, and unset it for the corresponding end tag.</p> 1377 1378<p>A handler may be <em>unset</em> by providing a <code>NULL</code> pointer to the 1379appropriate handler setter. None of the handler setting functions have 1380a return value.</p> 1381 1382<p>Your handlers will be receiving strings in arrays of type 1383<code>XML_Char</code>. This type is conditionally defined in expat.h as 1384either <code>char</code>, <code>wchar_t</code> or <code>unsigned short</code>. 1385The former implies UTF-8 encoding, the latter two imply UTF-16 encoding. 1386Note that you'll receive them in this form independent of the original 1387encoding of the document.</p> 1388 1389<div class="handler"> 1390<h4 id="XML_SetStartElementHandler">XML_SetStartElementHandler</h4> 1391<pre class="setter"> 1392void XMLCALL 1393XML_SetStartElementHandler(XML_Parser p, 1394 XML_StartElementHandler start); 1395</pre> 1396<pre class="signature"> 1397typedef void 1398(XMLCALL *XML_StartElementHandler)(void *userData, 1399 const XML_Char *name, 1400 const XML_Char **atts); 1401</pre> 1402<p>Set handler for start (and empty) tags. Attributes are passed to the start 1403handler as a pointer to a vector of char pointers. Each attribute seen in 1404a start (or empty) tag occupies 2 consecutive places in this vector: the 1405attribute name followed by the attribute value. These pairs are terminated 1406by a <code>NULL</code> pointer.</p> 1407<p>Note that an empty tag generates a call to both start and end handlers 1408(in that order).</p> 1409</div> 1410 1411<div class="handler"> 1412<h4 id="XML_SetEndElementHandler">XML_SetEndElementHandler</h4> 1413<pre class="setter"> 1414void XMLCALL 1415XML_SetEndElementHandler(XML_Parser p, 1416 XML_EndElementHandler); 1417</pre> 1418<pre class="signature"> 1419typedef void 1420(XMLCALL *XML_EndElementHandler)(void *userData, 1421 const XML_Char *name); 1422</pre> 1423<p>Set handler for end (and empty) tags. As noted above, an empty tag 1424generates a call to both start and end handlers.</p> 1425</div> 1426 1427<div class="handler"> 1428<h4 id="XML_SetElementHandler">XML_SetElementHandler</h4> 1429<pre class="setter"> 1430void XMLCALL 1431XML_SetElementHandler(XML_Parser p, 1432 XML_StartElementHandler start, 1433 XML_EndElementHandler end); 1434</pre> 1435<p>Set handlers for start and end tags with one call.</p> 1436</div> 1437 1438<div class="handler"> 1439<h4 id="XML_SetCharacterDataHandler">XML_SetCharacterDataHandler</h4> 1440<pre class="setter"> 1441void XMLCALL 1442XML_SetCharacterDataHandler(XML_Parser p, 1443 XML_CharacterDataHandler charhndl) 1444</pre> 1445<pre class="signature"> 1446typedef void 1447(XMLCALL *XML_CharacterDataHandler)(void *userData, 1448 const XML_Char *s, 1449 int len); 1450</pre> 1451<p>Set a text handler. The string your handler receives 1452is <em>NOT null-terminated</em>. You have to use the length argument 1453to deal with the end of the string. A single block of contiguous text 1454free of markup may still result in a sequence of calls to this handler. 1455In other words, if you're searching for a pattern in the text, it may 1456be split across calls to this handler. Note: Setting this handler to <code>NULL</code> 1457may <em>NOT immediately</em> terminate call-backs if the parser is currently 1458processing such a single block of contiguous markup-free text, as the parser 1459will continue calling back until the end of the block is reached.</p> 1460</div> 1461 1462<div class="handler"> 1463<h4 id="XML_SetProcessingInstructionHandler">XML_SetProcessingInstructionHandler</h4> 1464<pre class="setter"> 1465void XMLCALL 1466XML_SetProcessingInstructionHandler(XML_Parser p, 1467 XML_ProcessingInstructionHandler proc) 1468</pre> 1469<pre class="signature"> 1470typedef void 1471(XMLCALL *XML_ProcessingInstructionHandler)(void *userData, 1472 const XML_Char *target, 1473 const XML_Char *data); 1474 1475</pre> 1476<p>Set a handler for processing instructions. The target is the first word 1477in the processing instruction. The data is the rest of the characters in 1478it after skipping all whitespace after the initial word.</p> 1479</div> 1480 1481<div class="handler"> 1482<h4 id="XML_SetCommentHandler">XML_SetCommentHandler</h4> 1483<pre class="setter"> 1484void XMLCALL 1485XML_SetCommentHandler(XML_Parser p, 1486 XML_CommentHandler cmnt) 1487</pre> 1488<pre class="signature"> 1489typedef void 1490(XMLCALL *XML_CommentHandler)(void *userData, 1491 const XML_Char *data); 1492</pre> 1493<p>Set a handler for comments. The data is all text inside the comment 1494delimiters.</p> 1495</div> 1496 1497<div class="handler"> 1498<h4 id="XML_SetStartCdataSectionHandler">XML_SetStartCdataSectionHandler</h4> 1499<pre class="setter"> 1500void XMLCALL 1501XML_SetStartCdataSectionHandler(XML_Parser p, 1502 XML_StartCdataSectionHandler start); 1503</pre> 1504<pre class="signature"> 1505typedef void 1506(XMLCALL *XML_StartCdataSectionHandler)(void *userData); 1507</pre> 1508<p>Set a handler that gets called at the beginning of a CDATA section.</p> 1509</div> 1510 1511<div class="handler"> 1512<h4 id="XML_SetEndCdataSectionHandler">XML_SetEndCdataSectionHandler</h4> 1513<pre class="setter"> 1514void XMLCALL 1515XML_SetEndCdataSectionHandler(XML_Parser p, 1516 XML_EndCdataSectionHandler end); 1517</pre> 1518<pre class="signature"> 1519typedef void 1520(XMLCALL *XML_EndCdataSectionHandler)(void *userData); 1521</pre> 1522<p>Set a handler that gets called at the end of a CDATA section.</p> 1523</div> 1524 1525<div class="handler"> 1526<h4 id="XML_SetCdataSectionHandler">XML_SetCdataSectionHandler</h4> 1527<pre class="setter"> 1528void XMLCALL 1529XML_SetCdataSectionHandler(XML_Parser p, 1530 XML_StartCdataSectionHandler start, 1531 XML_EndCdataSectionHandler end) 1532</pre> 1533<p>Sets both CDATA section handlers with one call.</p> 1534</div> 1535 1536<div class="handler"> 1537<h4 id="XML_SetDefaultHandler">XML_SetDefaultHandler</h4> 1538<pre class="setter"> 1539void XMLCALL 1540XML_SetDefaultHandler(XML_Parser p, 1541 XML_DefaultHandler hndl) 1542</pre> 1543<pre class="signature"> 1544typedef void 1545(XMLCALL *XML_DefaultHandler)(void *userData, 1546 const XML_Char *s, 1547 int len); 1548</pre> 1549 1550<p>Sets a handler for any characters in the document which wouldn't 1551otherwise be handled. This includes both data for which no handlers 1552can be set (like some kinds of DTD declarations) and data which could 1553be reported but which currently has no handler set. The characters 1554are passed exactly as they were present in the XML document except 1555that they will be encoded in UTF-8 or UTF-16. Line boundaries are not 1556normalized. Note that a byte order mark character is not passed to the 1557default handler. There are no guarantees about how characters are 1558divided between calls to the default handler: for example, a comment 1559might be split between multiple calls. Setting the handler with 1560this call has the side effect of turning off expansion of references 1561to internally defined general entities. Instead these references are 1562passed to the default handler.</p> 1563 1564<p>See also <code><a 1565href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>.</p> 1566</div> 1567 1568<div class="handler"> 1569<h4 id="XML_SetDefaultHandlerExpand">XML_SetDefaultHandlerExpand</h4> 1570<pre class="setter"> 1571void XMLCALL 1572XML_SetDefaultHandlerExpand(XML_Parser p, 1573 XML_DefaultHandler hndl) 1574</pre> 1575<pre class="signature"> 1576typedef void 1577(XMLCALL *XML_DefaultHandler)(void *userData, 1578 const XML_Char *s, 1579 int len); 1580</pre> 1581<p>This sets a default handler, but doesn't inhibit the expansion of 1582internal entity references. The entity reference will not be passed 1583to the default handler.</p> 1584 1585<p>See also <code><a 1586href="#XML_DefaultCurrent">XML_DefaultCurrent</a></code>.</p> 1587</div> 1588 1589<div class="handler"> 1590<h4 id="XML_SetExternalEntityRefHandler">XML_SetExternalEntityRefHandler</h4> 1591<pre class="setter"> 1592void XMLCALL 1593XML_SetExternalEntityRefHandler(XML_Parser p, 1594 XML_ExternalEntityRefHandler hndl) 1595</pre> 1596<pre class="signature"> 1597typedef int 1598(XMLCALL *XML_ExternalEntityRefHandler)(XML_Parser p, 1599 const XML_Char *context, 1600 const XML_Char *base, 1601 const XML_Char *systemId, 1602 const XML_Char *publicId); 1603</pre> 1604<p>Set an external entity reference handler. This handler is also 1605called for processing an external DTD subset if parameter entity parsing 1606is in effect. (See <a href="#XML_SetParamEntityParsing"> 1607<code>XML_SetParamEntityParsing</code></a>.)</p> 1608 1609<p>The <code>context</code> parameter specifies the parsing context in 1610the format expected by the <code>context</code> argument to <code><a 1611href="#XML_ExternalEntityParserCreate" 1612>XML_ExternalEntityParserCreate</a></code>. <code>code</code> is 1613valid only until the handler returns, so if the referenced entity is 1614to be parsed later, it must be copied. <code>context</code> is <code>NULL</code> 1615only when the entity is a parameter entity, which is how one can 1616differentiate between general and parameter entities.</p> 1617 1618<p>The <code>base</code> parameter is the base to use for relative 1619system identifiers. It is set by <code><a 1620href="#XML_SetBase">XML_SetBase</a></code> and may be <code>NULL</code>. The 1621<code>publicId</code> parameter is the public id given in the entity 1622declaration and may be <code>NULL</code>. <code>systemId</code> is the system 1623identifier specified in the entity declaration and is never <code>NULL</code>.</p> 1624 1625<p>There are a couple of ways in which this handler differs from 1626others. First, this handler returns a status indicator (an 1627integer). <code>XML_STATUS_OK</code> should be returned for successful 1628handling of the external entity reference. Returning 1629<code>XML_STATUS_ERROR</code> indicates failure, and causes the 1630calling parser to return an 1631<code>XML_ERROR_EXTERNAL_ENTITY_HANDLING</code> error.</p> 1632 1633<p>Second, instead of having the user data as its first argument, it 1634receives the parser that encountered the entity reference. This, along 1635with the context parameter, may be used as arguments to a call to 1636<code><a href= "#XML_ExternalEntityParserCreate" 1637>XML_ExternalEntityParserCreate</a></code>. Using the returned 1638parser, the body of the external entity can be recursively parsed.</p> 1639 1640<p>Since this handler may be called recursively, it should not be saving 1641information into global or static variables.</p> 1642</div> 1643 1644<h4 id="XML_SetExternalEntityRefHandlerArg">XML_SetExternalEntityRefHandlerArg</h4> 1645<pre class="fcndec"> 1646void XMLCALL 1647XML_SetExternalEntityRefHandlerArg(XML_Parser p, 1648 void *arg) 1649</pre> 1650<div class="fcndef"> 1651<p>Set the argument passed to the ExternalEntityRefHandler. If 1652<code>arg</code> is not <code>NULL</code>, it is the new value passed to the 1653handler set using <code><a href="#XML_SetExternalEntityRefHandler" 1654>XML_SetExternalEntityRefHandler</a></code>; if <code>arg</code> is 1655<code>NULL</code>, the argument passed to the handler function will be the parser 1656object itself.</p> 1657 1658<p><strong>Note:</strong> 1659The type of <code>arg</code> and the type of the first argument to the 1660ExternalEntityRefHandler do not match. This function takes a 1661<code>void *</code> to be passed to the handler, while the handler 1662accepts an <code>XML_Parser</code>. This is a historical accident, 1663but will not be corrected before Expat 2.0 (at the earliest) to avoid 1664causing compiler warnings for code that's known to work with this 1665API. It is the responsibility of the application code to know the 1666actual type of the argument passed to the handler and to manage it 1667properly.</p> 1668</div> 1669 1670<div class="handler"> 1671<h4 id="XML_SetSkippedEntityHandler">XML_SetSkippedEntityHandler</h4> 1672<pre class="setter"> 1673void XMLCALL 1674XML_SetSkippedEntityHandler(XML_Parser p, 1675 XML_SkippedEntityHandler handler) 1676</pre> 1677<pre class="signature"> 1678typedef void 1679(XMLCALL *XML_SkippedEntityHandler)(void *userData, 1680 const XML_Char *entityName, 1681 int is_parameter_entity); 1682</pre> 1683<p>Set a skipped entity handler. This is called in two situations:</p> 1684<ol> 1685 <li>An entity reference is encountered for which no declaration 1686 has been read <em>and</em> this is not an error.</li> 1687 <li>An internal entity reference is read, but not expanded, because 1688 <a href="#XML_SetDefaultHandler"><code>XML_SetDefaultHandler</code></a> 1689 has been called.</li> 1690</ol> 1691<p>The <code>is_parameter_entity</code> argument will be non-zero for 1692a parameter entity and zero for a general entity.</p> <p>Note: Skipped 1693parameter entities in declarations and skipped general entities in 1694attribute values cannot be reported, because the event would be out of 1695sync with the reporting of the declarations or attribute values</p> 1696</div> 1697 1698<div class="handler"> 1699<h4 id="XML_SetUnknownEncodingHandler">XML_SetUnknownEncodingHandler</h4> 1700<pre class="setter"> 1701void XMLCALL 1702XML_SetUnknownEncodingHandler(XML_Parser p, 1703 XML_UnknownEncodingHandler enchandler, 1704 void *encodingHandlerData) 1705</pre> 1706<pre class="signature"> 1707typedef int 1708(XMLCALL *XML_UnknownEncodingHandler)(void *encodingHandlerData, 1709 const XML_Char *name, 1710 XML_Encoding *info); 1711 1712typedef struct { 1713 int map[256]; 1714 void *data; 1715 int (XMLCALL *convert)(void *data, const char *s); 1716 void (XMLCALL *release)(void *data); 1717} XML_Encoding; 1718</pre> 1719<p>Set a handler to deal with encodings other than the <a 1720href="#builtin_encodings">built in set</a>. This should be done before 1721<code><a href= "#XML_Parse" >XML_Parse</a></code> or <code><a href= 1722"#XML_ParseBuffer" >XML_ParseBuffer</a></code> have been called on the 1723given parser.</p> <p>If the handler knows how to deal with an encoding 1724with the given name, it should fill in the <code>info</code> data 1725structure and return <code>XML_STATUS_OK</code>. Otherwise it 1726should return <code>XML_STATUS_ERROR</code>. The handler will be called 1727at most once per parsed (external) entity. The optional application 1728data pointer <code>encodingHandlerData</code> will be passed back to 1729the handler.</p> 1730 1731<p>The map array contains information for every possible leading 1732byte in a byte sequence. If the corresponding value is >= 0, then it's 1733a single byte sequence and the byte encodes that Unicode value. If the 1734value is -1, then that byte is invalid as the initial byte in a sequence. 1735If the value is -n, where n is an integer > 1, then n is the number of 1736bytes in the sequence and the actual conversion is accomplished by a 1737call to the function pointed at by convert. This function may return -1 1738if the sequence itself is invalid. The convert pointer may be <code>NULL</code> if 1739there are only single byte codes. The data parameter passed to the convert 1740function is the data pointer from <code>XML_Encoding</code>. The 1741string s is <em>NOT</em> null-terminated and points at the sequence of 1742bytes to be converted.</p> 1743 1744<p>The function pointed at by <code>release</code> is called by the 1745parser when it is finished with the encoding. It may be <code>NULL</code>.</p> 1746</div> 1747 1748<div class="handler"> 1749<h4 id="XML_SetStartNamespaceDeclHandler">XML_SetStartNamespaceDeclHandler</h4> 1750<pre class="setter"> 1751void XMLCALL 1752XML_SetStartNamespaceDeclHandler(XML_Parser p, 1753 XML_StartNamespaceDeclHandler start); 1754</pre> 1755<pre class="signature"> 1756typedef void 1757(XMLCALL *XML_StartNamespaceDeclHandler)(void *userData, 1758 const XML_Char *prefix, 1759 const XML_Char *uri); 1760</pre> 1761<p>Set a handler to be called when a namespace is declared. Namespace 1762declarations occur inside start tags. But the namespace declaration start 1763handler is called before the start tag handler for each namespace declared 1764in that start tag.</p> 1765</div> 1766 1767<div class="handler"> 1768<h4 id="XML_SetEndNamespaceDeclHandler">XML_SetEndNamespaceDeclHandler</h4> 1769<pre class="setter"> 1770void XMLCALL 1771XML_SetEndNamespaceDeclHandler(XML_Parser p, 1772 XML_EndNamespaceDeclHandler end); 1773</pre> 1774<pre class="signature"> 1775typedef void 1776(XMLCALL *XML_EndNamespaceDeclHandler)(void *userData, 1777 const XML_Char *prefix); 1778</pre> 1779<p>Set a handler to be called when leaving the scope of a namespace 1780declaration. This will be called, for each namespace declaration, 1781after the handler for the end tag of the element in which the 1782namespace was declared.</p> 1783</div> 1784 1785<div class="handler"> 1786<h4 id="XML_SetNamespaceDeclHandler">XML_SetNamespaceDeclHandler</h4> 1787<pre class="setter"> 1788void XMLCALL 1789XML_SetNamespaceDeclHandler(XML_Parser p, 1790 XML_StartNamespaceDeclHandler start, 1791 XML_EndNamespaceDeclHandler end) 1792</pre> 1793<p>Sets both namespace declaration handlers with a single call.</p> 1794</div> 1795 1796<div class="handler"> 1797<h4 id="XML_SetXmlDeclHandler">XML_SetXmlDeclHandler</h4> 1798<pre class="setter"> 1799void XMLCALL 1800XML_SetXmlDeclHandler(XML_Parser p, 1801 XML_XmlDeclHandler xmldecl); 1802</pre> 1803<pre class="signature"> 1804typedef void 1805(XMLCALL *XML_XmlDeclHandler)(void *userData, 1806 const XML_Char *version, 1807 const XML_Char *encoding, 1808 int standalone); 1809</pre> 1810<p>Sets a handler that is called for XML declarations and also for 1811text declarations discovered in external entities. The way to 1812distinguish is that the <code>version</code> parameter will be <code>NULL</code> 1813for text declarations. The <code>encoding</code> parameter may be <code>NULL</code> 1814for an XML declaration. The <code>standalone</code> argument will 1815contain -1, 0, or 1 indicating respectively that there was no 1816standalone parameter in the declaration, that it was given as no, or 1817that it was given as yes.</p> 1818</div> 1819 1820<div class="handler"> 1821<h4 id="XML_SetStartDoctypeDeclHandler">XML_SetStartDoctypeDeclHandler</h4> 1822<pre class="setter"> 1823void XMLCALL 1824XML_SetStartDoctypeDeclHandler(XML_Parser p, 1825 XML_StartDoctypeDeclHandler start); 1826</pre> 1827<pre class="signature"> 1828typedef void 1829(XMLCALL *XML_StartDoctypeDeclHandler)(void *userData, 1830 const XML_Char *doctypeName, 1831 const XML_Char *sysid, 1832 const XML_Char *pubid, 1833 int has_internal_subset); 1834</pre> 1835<p>Set a handler that is called at the start of a DOCTYPE declaration, 1836before any external or internal subset is parsed. Both <code>sysid</code> 1837and <code>pubid</code> may be <code>NULL</code>. The <code>has_internal_subset</code> 1838will be non-zero if the DOCTYPE declaration has an internal subset.</p> 1839</div> 1840 1841<div class="handler"> 1842<h4 id="XML_SetEndDoctypeDeclHandler">XML_SetEndDoctypeDeclHandler</h4> 1843<pre class="setter"> 1844void XMLCALL 1845XML_SetEndDoctypeDeclHandler(XML_Parser p, 1846 XML_EndDoctypeDeclHandler end); 1847</pre> 1848<pre class="signature"> 1849typedef void 1850(XMLCALL *XML_EndDoctypeDeclHandler)(void *userData); 1851</pre> 1852<p>Set a handler that is called at the end of a DOCTYPE declaration, 1853after parsing any external subset.</p> 1854</div> 1855 1856<div class="handler"> 1857<h4 id="XML_SetDoctypeDeclHandler">XML_SetDoctypeDeclHandler</h4> 1858<pre class="setter"> 1859void XMLCALL 1860XML_SetDoctypeDeclHandler(XML_Parser p, 1861 XML_StartDoctypeDeclHandler start, 1862 XML_EndDoctypeDeclHandler end); 1863</pre> 1864<p>Set both doctype handlers with one call.</p> 1865</div> 1866 1867<div class="handler"> 1868<h4 id="XML_SetElementDeclHandler">XML_SetElementDeclHandler</h4> 1869<pre class="setter"> 1870void XMLCALL 1871XML_SetElementDeclHandler(XML_Parser p, 1872 XML_ElementDeclHandler eldecl); 1873</pre> 1874<pre class="signature"> 1875typedef void 1876(XMLCALL *XML_ElementDeclHandler)(void *userData, 1877 const XML_Char *name, 1878 XML_Content *model); 1879</pre> 1880<pre class="signature"> 1881enum XML_Content_Type { 1882 XML_CTYPE_EMPTY = 1, 1883 XML_CTYPE_ANY, 1884 XML_CTYPE_MIXED, 1885 XML_CTYPE_NAME, 1886 XML_CTYPE_CHOICE, 1887 XML_CTYPE_SEQ 1888}; 1889 1890enum XML_Content_Quant { 1891 XML_CQUANT_NONE, 1892 XML_CQUANT_OPT, 1893 XML_CQUANT_REP, 1894 XML_CQUANT_PLUS 1895}; 1896 1897typedef struct XML_cp XML_Content; 1898 1899struct XML_cp { 1900 enum XML_Content_Type type; 1901 enum XML_Content_Quant quant; 1902 const XML_Char * name; 1903 unsigned int numchildren; 1904 XML_Content * children; 1905}; 1906</pre> 1907<p>Sets a handler for element declarations in a DTD. The handler gets 1908called with the name of the element in the declaration and a pointer 1909to a structure that contains the element model. It's the user code's 1910responsibility to free model when finished with via a call to <code> 1911<a href="#XML_FreeContentModel">XML_FreeContentModel</a></code>. 1912There is no need to free the model from the handler, it can be kept 1913around and freed at a later stage.</p> 1914 1915<p>The <code>model</code> argument is the root of a tree of 1916<code>XML_Content</code> nodes. If <code>type</code> equals 1917<code>XML_CTYPE_EMPTY</code> or <code>XML_CTYPE_ANY</code>, then 1918<code>quant</code> will be <code>XML_CQUANT_NONE</code>, and the other 1919fields will be zero or <code>NULL</code>. If <code>type</code> is 1920<code>XML_CTYPE_MIXED</code>, then <code>quant</code> will be 1921<code>XML_CQUANT_NONE</code> or <code>XML_CQUANT_REP</code> and 1922<code>numchildren</code> will contain the number of elements that are 1923allowed to be mixed in and <code>children</code> points to an array of 1924<code>XML_Content</code> structures that will all have type 1925XML_CTYPE_NAME with no quantification. Only the root node can be type 1926<code>XML_CTYPE_EMPTY</code>, <code>XML_CTYPE_ANY</code>, or 1927<code>XML_CTYPE_MIXED</code>.</p> 1928 1929<p>For type <code>XML_CTYPE_NAME</code>, the <code>name</code> field 1930points to the name and the <code>numchildren</code> and 1931<code>children</code> fields will be zero and <code>NULL</code>. The 1932<code>quant</code> field will indicate any quantifiers placed on the 1933name.</p> 1934 1935<p>Types <code>XML_CTYPE_CHOICE</code> and <code>XML_CTYPE_SEQ</code> 1936indicate a choice or sequence respectively. The 1937<code>numchildren</code> field indicates how many nodes in the choice 1938or sequence and <code>children</code> points to the nodes.</p> 1939</div> 1940 1941<div class="handler"> 1942<h4 id="XML_SetAttlistDeclHandler">XML_SetAttlistDeclHandler</h4> 1943<pre class="setter"> 1944void XMLCALL 1945XML_SetAttlistDeclHandler(XML_Parser p, 1946 XML_AttlistDeclHandler attdecl); 1947</pre> 1948<pre class="signature"> 1949typedef void 1950(XMLCALL *XML_AttlistDeclHandler)(void *userData, 1951 const XML_Char *elname, 1952 const XML_Char *attname, 1953 const XML_Char *att_type, 1954 const XML_Char *dflt, 1955 int isrequired); 1956</pre> 1957<p>Set a handler for attlist declarations in the DTD. This handler is 1958called for <em>each</em> attribute. So a single attlist declaration 1959with multiple attributes declared will generate multiple calls to this 1960handler. The <code>elname</code> parameter returns the name of the 1961element for which the attribute is being declared. The attribute name 1962is in the <code>attname</code> parameter. The attribute type is in the 1963<code>att_type</code> parameter. It is the string representing the 1964type in the declaration with whitespace removed.</p> 1965 1966<p>The <code>dflt</code> parameter holds the default value. It will be 1967<code>NULL</code> in the case of "#IMPLIED" or "#REQUIRED" attributes. You can 1968distinguish these two cases by checking the <code>isrequired</code> 1969parameter, which will be true in the case of "#REQUIRED" attributes. 1970Attributes which are "#FIXED" will have also have a true 1971<code>isrequired</code>, but they will have the non-<code>NULL</code> fixed value 1972in the <code>dflt</code> parameter.</p> 1973</div> 1974 1975<div class="handler"> 1976<h4 id="XML_SetEntityDeclHandler">XML_SetEntityDeclHandler</h4> 1977<pre class="setter"> 1978void XMLCALL 1979XML_SetEntityDeclHandler(XML_Parser p, 1980 XML_EntityDeclHandler handler); 1981</pre> 1982<pre class="signature"> 1983typedef void 1984(XMLCALL *XML_EntityDeclHandler)(void *userData, 1985 const XML_Char *entityName, 1986 int is_parameter_entity, 1987 const XML_Char *value, 1988 int value_length, 1989 const XML_Char *base, 1990 const XML_Char *systemId, 1991 const XML_Char *publicId, 1992 const XML_Char *notationName); 1993</pre> 1994<p>Sets a handler that will be called for all entity declarations. 1995The <code>is_parameter_entity</code> argument will be non-zero in the 1996case of parameter entities and zero otherwise.</p> 1997 1998<p>For internal entities (<code><!ENTITY foo "bar"></code>), 1999<code>value</code> will be non-<code>NULL</code> and <code>systemId</code>, 2000<code>publicId</code>, and <code>notationName</code> will all be <code>NULL</code>. 2001The value string is <em>not</em> null-terminated; the length is 2002provided in the <code>value_length</code> parameter. Do not use 2003<code>value_length</code> to test for internal entities, since it is 2004legal to have zero-length values. Instead check for whether or not 2005<code>value</code> is <code>NULL</code>.</p> <p>The <code>notationName</code> 2006argument will have a non-<code>NULL</code> value only for unparsed entity 2007declarations.</p> 2008</div> 2009 2010<div class="handler"> 2011<h4 id="XML_SetUnparsedEntityDeclHandler">XML_SetUnparsedEntityDeclHandler</h4> 2012<pre class="setter"> 2013void XMLCALL 2014XML_SetUnparsedEntityDeclHandler(XML_Parser p, 2015 XML_UnparsedEntityDeclHandler h) 2016</pre> 2017<pre class="signature"> 2018typedef void 2019(XMLCALL *XML_UnparsedEntityDeclHandler)(void *userData, 2020 const XML_Char *entityName, 2021 const XML_Char *base, 2022 const XML_Char *systemId, 2023 const XML_Char *publicId, 2024 const XML_Char *notationName); 2025</pre> 2026<p>Set a handler that receives declarations of unparsed entities. These 2027are entity declarations that have a notation (NDATA) field:</p> 2028 2029<div id="eg"><pre> 2030<!ENTITY logo SYSTEM "images/logo.gif" NDATA gif> 2031</pre></div> 2032<p>This handler is obsolete and is provided for backwards 2033compatibility. Use instead <a href= "#XML_SetEntityDeclHandler" 2034>XML_SetEntityDeclHandler</a>.</p> 2035</div> 2036 2037<div class="handler"> 2038<h4 id="XML_SetNotationDeclHandler">XML_SetNotationDeclHandler</h4> 2039<pre class="setter"> 2040void XMLCALL 2041XML_SetNotationDeclHandler(XML_Parser p, 2042 XML_NotationDeclHandler h) 2043</pre> 2044<pre class="signature"> 2045typedef void 2046(XMLCALL *XML_NotationDeclHandler)(void *userData, 2047 const XML_Char *notationName, 2048 const XML_Char *base, 2049 const XML_Char *systemId, 2050 const XML_Char *publicId); 2051</pre> 2052<p>Set a handler that receives notation declarations.</p> 2053</div> 2054 2055<div class="handler"> 2056<h4 id="XML_SetNotStandaloneHandler">XML_SetNotStandaloneHandler</h4> 2057<pre class="setter"> 2058void XMLCALL 2059XML_SetNotStandaloneHandler(XML_Parser p, 2060 XML_NotStandaloneHandler h) 2061</pre> 2062<pre class="signature"> 2063typedef int 2064(XMLCALL *XML_NotStandaloneHandler)(void *userData); 2065</pre> 2066<p>Set a handler that is called if the document is not "standalone". 2067This happens when there is an external subset or a reference to a 2068parameter entity, but does not have standalone set to "yes" in an XML 2069declaration. If this handler returns <code>XML_STATUS_ERROR</code>, 2070then the parser will throw an <code>XML_ERROR_NOT_STANDALONE</code> 2071error.</p> 2072</div> 2073 2074<h3><a name="position">Parse position and error reporting functions</a></h3> 2075 2076<p>These are the functions you'll want to call when the parse 2077functions return <code>XML_STATUS_ERROR</code> (a parse error has 2078occurred), although the position reporting functions are useful outside 2079of errors. The position reported is the byte position (in the original 2080document or entity encoding) of the first of the sequence of 2081characters that generated the current event (or the error that caused 2082the parse functions to return <code>XML_STATUS_ERROR</code>.) The 2083exceptions are callbacks triggered by declarations in the document 2084prologue, in which case they exact position reported is somewhere in the 2085relevant markup, but not necessarily as meaningful as for other 2086events.</p> 2087 2088<p>The position reporting functions are accurate only outside of the 2089DTD. In other words, they usually return bogus information when 2090called from within a DTD declaration handler.</p> 2091 2092<h4 id="XML_GetErrorCode">XML_GetErrorCode</h4> 2093<pre class="fcndec"> 2094enum XML_Error XMLCALL 2095XML_GetErrorCode(XML_Parser p); 2096</pre> 2097<div class="fcndef"> 2098Return what type of error has occurred. 2099</div> 2100 2101<h4 id="XML_ErrorString">XML_ErrorString</h4> 2102<pre class="fcndec"> 2103const XML_LChar * XMLCALL 2104XML_ErrorString(enum XML_Error code); 2105</pre> 2106<div class="fcndef"> 2107Return a string describing the error corresponding to code. 2108The code should be one of the enums that can be returned from 2109<code><a href= "#XML_GetErrorCode" >XML_GetErrorCode</a></code>. 2110</div> 2111 2112<h4 id="XML_GetCurrentByteIndex">XML_GetCurrentByteIndex</h4> 2113<pre class="fcndec"> 2114XML_Index XMLCALL 2115XML_GetCurrentByteIndex(XML_Parser p); 2116</pre> 2117<div class="fcndef"> 2118Return the byte offset of the position. This always corresponds to 2119the values returned by <code><a href= "#XML_GetCurrentLineNumber" 2120>XML_GetCurrentLineNumber</a></code> and <code><a href= 2121"#XML_GetCurrentColumnNumber" >XML_GetCurrentColumnNumber</a></code>. 2122</div> 2123 2124<h4 id="XML_GetCurrentLineNumber">XML_GetCurrentLineNumber</h4> 2125<pre class="fcndec"> 2126XML_Size XMLCALL 2127XML_GetCurrentLineNumber(XML_Parser p); 2128</pre> 2129<div class="fcndef"> 2130Return the line number of the position. The first line is reported as 2131<code>1</code>. 2132</div> 2133 2134<h4 id="XML_GetCurrentColumnNumber">XML_GetCurrentColumnNumber</h4> 2135<pre class="fcndec"> 2136XML_Size XMLCALL 2137XML_GetCurrentColumnNumber(XML_Parser p); 2138</pre> 2139<div class="fcndef"> 2140Return the <em>offset</em>, from the beginning of the current line, of 2141the position. The first column is reported as <code>0</code>. 2142</div> 2143 2144<h4 id="XML_GetCurrentByteCount">XML_GetCurrentByteCount</h4> 2145<pre class="fcndec"> 2146int XMLCALL 2147XML_GetCurrentByteCount(XML_Parser p); 2148</pre> 2149<div class="fcndef"> 2150Return the number of bytes in the current event. Returns 2151<code>0</code> if the event is inside a reference to an internal 2152entity and for the end-tag event for empty element tags (the later can 2153be used to distinguish empty-element tags from empty elements using 2154separate start and end tags). 2155</div> 2156 2157<h4 id="XML_GetInputContext">XML_GetInputContext</h4> 2158<pre class="fcndec"> 2159const char * XMLCALL 2160XML_GetInputContext(XML_Parser p, 2161 int *offset, 2162 int *size); 2163</pre> 2164<div class="fcndef"> 2165 2166<p>Returns the parser's input buffer, sets the integer pointed at by 2167<code>offset</code> to the offset within this buffer of the current 2168parse position, and set the integer pointed at by <code>size</code> to 2169the size of the returned buffer.</p> 2170 2171<p>This should only be called from within a handler during an active 2172parse and the returned buffer should only be referred to from within 2173the handler that made the call. This input buffer contains the 2174untranslated bytes of the input.</p> 2175 2176<p>Only a limited amount of context is kept, so if the event 2177triggering a call spans over a very large amount of input, the actual 2178parse position may be before the beginning of the buffer.</p> 2179 2180<p>If <code>XML_CONTEXT_BYTES</code> is zero, this will always 2181return <code>NULL</code>.</p> 2182</div> 2183 2184<h3><a name="attack-protection">Attack Protection</a><a name="billion-laughs"></a></h3> 2185 2186<h4 id="XML_SetBillionLaughsAttackProtectionMaximumAmplification">XML_SetBillionLaughsAttackProtectionMaximumAmplification</h4> 2187<pre class="fcndec"> 2188/* Added in Expat 2.4.0. */ 2189XML_Bool XMLCALL 2190XML_SetBillionLaughsAttackProtectionMaximumAmplification(XML_Parser p, 2191 float maximumAmplificationFactor); 2192</pre> 2193<div class="fcndef"> 2194 <p> 2195 Sets the maximum tolerated amplification factor 2196 for protection against 2197 <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a> 2198 (default: <code>100.0</code>) 2199 of parser <code>p</code> to <code>maximumAmplificationFactor</code>, and 2200 returns <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error. 2201 </p> 2202 2203 <p> 2204 Once the <a href="#XML_SetBillionLaughsAttackProtectionActivationThreshold">threshold for activation</a> is reached, 2205 the amplification factor is calculated as .. 2206 </p> 2207 <pre>amplification := (direct + indirect) / direct</pre> 2208 <p> 2209 .. while parsing, whereas 2210 <code>direct</code> is the number of bytes read from the primary document in parsing and 2211 <code>indirect</code> is the number of bytes added by expanding entities and reading of external DTD files, combined. 2212 </p> 2213 2214 <p>For a call to <code>XML_SetBillionLaughsAttackProtectionMaximumAmplification</code> to succeed:</p> 2215 <ul> 2216 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers) and</li> 2217 <li><code>maximumAmplificationFactor</code> must be non-<code>NaN</code> and greater than or equal to <code>1.0</code>.</li> 2218 </ul> 2219 2220 <p> 2221 <strong>Note:</strong> 2222 If you ever need to increase this value for non-attack payload, 2223 please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>. 2224 </p> 2225 2226 <p> 2227 <strong>Note:</strong> 2228 Peak amplifications 2229 of factor 15,000 for the entire payload and 2230 of factor 30,000 in the middle of parsing 2231 have been observed with small benign files in practice. 2232 2233 So if you do reduce the maximum allowed amplification, 2234 please make sure that the activation threshold is still big enough 2235 to not end up with undesired false positives (i.e. benign files being rejected). 2236 </p> 2237</div> 2238 2239<h4 id="XML_SetBillionLaughsAttackProtectionActivationThreshold">XML_SetBillionLaughsAttackProtectionActivationThreshold</h4> 2240<pre class="fcndec"> 2241/* Added in Expat 2.4.0. */ 2242XML_Bool XMLCALL 2243XML_SetBillionLaughsAttackProtectionActivationThreshold(XML_Parser p, 2244 unsigned long long activationThresholdBytes); 2245</pre> 2246<div class="fcndef"> 2247 <p> 2248 Sets number of output bytes (including amplification from entity expansion and reading DTD files) 2249 needed to activate protection against 2250 <a href="https://en.wikipedia.org/wiki/Billion_laughs_attack">billion laughs attacks</a> 2251 (default: <code>8 MiB</code>) 2252 of parser <code>p</code> to <code>activationThresholdBytes</code>, and 2253 returns <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error. 2254 </p> 2255 2256 <p>For a call to <code>XML_SetBillionLaughsAttackProtectionActivationThreshold</code> to succeed:</p> 2257 <ul> 2258 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers).</li> 2259 </ul> 2260 2261 <p> 2262 <strong>Note:</strong> 2263 If you ever need to increase this value for non-attack payload, 2264 please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>. 2265 </p> 2266 2267 <p> 2268 <strong>Note:</strong> 2269 Activation thresholds below 4 MiB are known to break support for 2270 <a href="https://en.wikipedia.org/wiki/Darwin_Information_Typing_Architecture">DITA</a> 1.3 payload 2271 and are hence not recommended. 2272 </p> 2273</div> 2274 2275<h4 id="XML_SetAllocTrackerMaximumAmplification">XML_SetAllocTrackerMaximumAmplification</h4> 2276<pre class="fcndec"> 2277/* Added in Expat 2.7.2. */ 2278XML_Bool 2279XML_SetAllocTrackerMaximumAmplification(XML_Parser p, 2280 float maximumAmplificationFactor); 2281</pre> 2282<div class="fcndef"> 2283 <p> 2284 Sets the maximum tolerated amplification factor 2285 between direct input and bytes of dynamic memory allocated 2286 (default: <code>100.0</code>) 2287 of parser <code>p</code> to <code>maximumAmplificationFactor</code>, and 2288 returns <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error. 2289 </p> 2290 2291 <p> 2292 <strong>Note:</strong> 2293 There are three types of allocations that intentionally bypass tracking and limiting: 2294 </p> 2295 <ul> 2296 <li> 2297 application calls to functions 2298 <code><a href="#XML_MemMalloc">XML_MemMalloc</a></code> 2299 and 2300 <code><a href="#XML_MemRealloc">XML_MemRealloc</a></code> 2301 — 2302 <em>healthy</em> use of these two functions continues to be a responsibility 2303 of the application using Expat 2304 —, 2305 </li> 2306 <li> 2307 the main character buffer used by functions 2308 <code><a href="#XML_GetBuffer">XML_GetBuffer</a></code> 2309 and 2310 <code><a href="#XML_ParseBuffer">XML_ParseBuffer</a></code> 2311 (and thus also by plain 2312 <code><a href="#XML_Parse">XML_Parse</a></code>), and 2313 </li> 2314 <li> 2315 the <a href="#XML_SetElementDeclHandler">content model memory</a> 2316 (that is passed to the 2317 <a href="#XML_SetElementDeclHandler">element declaration handler</a> 2318 and freed by a call to 2319 <code><a href="#XML_FreeContentModel">XML_FreeContentModel</a></code>). 2320 </li> 2321 </ul> 2322 2323 <p> 2324 Once the <a href="#XML_SetAllocTrackerActivationThreshold">threshold for activation</a> is reached, 2325 the amplification factor is calculated as .. 2326 </p> 2327 <pre>amplification := allocated / direct</pre> 2328 <p> 2329 .. while parsing, whereas 2330 <code>direct</code> is the number of bytes read from the primary document in parsing and 2331 <code>allocated</code> is the number of bytes of dynamic memory allocated in the parser hierarchy. 2332 </p> 2333 2334 <p>For a call to <code>XML_SetAllocTrackerMaximumAmplification</code> to succeed:</p> 2335 <ul> 2336 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers) and</li> 2337 <li><code>maximumAmplificationFactor</code> must be non-<code>NaN</code> and greater than or equal to <code>1.0</code>.</li> 2338 </ul> 2339 2340 <p> 2341 <strong>Note:</strong> 2342 If you ever need to increase this value for non-attack payload, 2343 please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>. 2344 </p> 2345 2346 <p> 2347 <strong>Note:</strong> 2348 Amplifications factors greater than <code>100.0</code> can been observed near the start of parsing 2349 even with benign files in practice. 2350 2351 So if you do reduce the maximum allowed amplification, 2352 please make sure that the activation threshold is still big enough 2353 to not end up with undesired false positives (i.e. benign files being rejected). 2354 </p> 2355</div> 2356 2357<h4 id="XML_SetAllocTrackerActivationThreshold">XML_SetAllocTrackerActivationThreshold</h4> 2358<pre class="fcndec"> 2359/* Added in Expat 2.7.2. */ 2360XML_Bool 2361XML_SetAllocTrackerActivationThreshold(XML_Parser p, 2362 unsigned long long activationThresholdBytes); 2363</pre> 2364<div class="fcndef"> 2365 <p> 2366 Sets number of allocated bytes of dynamic memory 2367 needed to activate protection against disproportionate use of RAM 2368 (default: <code>64 MiB</code>) 2369 of parser <code>p</code> to <code>activationThresholdBytes</code>, and 2370 returns <code>XML_TRUE</code> upon success and <code>XML_FALSE</code> upon error. 2371 </p> 2372 2373 <p> 2374 <strong>Note:</strong> 2375 For types of allocations that intentionally bypass tracking and limiting, please see 2376 <code><a href="#XML_SetAllocTrackerMaximumAmplification">XML_SetAllocTrackerMaximumAmplification</a></code> 2377 above. 2378 </p> 2379 2380 <p>For a call to <code>XML_SetAllocTrackerActivationThreshold</code> to succeed:</p> 2381 <ul> 2382 <li>parser <code>p</code> must be a non-<code>NULL</code> root parser (without any parent parsers).</li> 2383 </ul> 2384 2385 <p> 2386 <strong>Note:</strong> 2387 If you ever need to increase this value for non-attack payload, 2388 please <a href="https://github.com/libexpat/libexpat/issues">file a bug report</a>. 2389 </p> 2390</div> 2391 2392<h4 id="XML_SetReparseDeferralEnabled">XML_SetReparseDeferralEnabled</h4> 2393<pre class="fcndec"> 2394/* Added in Expat 2.6.0. */ 2395XML_Bool XMLCALL 2396XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled); 2397</pre> 2398<div class="fcndef"> 2399 <p> 2400 Large tokens may require many parse calls before enough data is available for Expat to parse it in full. 2401 If Expat retried parsing the token on every parse call, parsing could take quadratic time. 2402 To avoid this, Expat only retries once a significant amount of new data is available. 2403 This function allows disabling this behavior. 2404 </p> 2405 <p> 2406 The <code>enabled</code> argument should be <code>XML_TRUE</code> or <code>XML_FALSE</code>. 2407 </p> 2408 <p> 2409 Returns <code>XML_TRUE</code> on success, and <code>XML_FALSE</code> on error. 2410 </p> 2411</div> 2412 2413<h3><a name="miscellaneous">Miscellaneous functions</a></h3> 2414 2415<p>The functions in this section either obtain state information from 2416the parser or can be used to dynamically set parser options.</p> 2417 2418<h4 id="XML_SetUserData">XML_SetUserData</h4> 2419<pre class="fcndec"> 2420void XMLCALL 2421XML_SetUserData(XML_Parser p, 2422 void *userData); 2423</pre> 2424<div class="fcndef"> 2425This sets the user data pointer that gets passed to handlers. It 2426overwrites any previous value for this pointer. Note that the 2427application is responsible for freeing the memory associated with 2428<code>userData</code> when it is finished with the parser. So if you 2429call this when there's already a pointer there, and you haven't freed 2430the memory associated with it, then you've probably just leaked 2431memory. 2432</div> 2433 2434<h4 id="XML_GetUserData">XML_GetUserData</h4> 2435<pre class="fcndec"> 2436void * XMLCALL 2437XML_GetUserData(XML_Parser p); 2438</pre> 2439<div class="fcndef"> 2440This returns the user data pointer that gets passed to handlers. 2441It is actually implemented as a macro. 2442</div> 2443 2444<h4 id="XML_UseParserAsHandlerArg">XML_UseParserAsHandlerArg</h4> 2445<pre class="fcndec"> 2446void XMLCALL 2447XML_UseParserAsHandlerArg(XML_Parser p); 2448</pre> 2449<div class="fcndef"> 2450After this is called, handlers receive the parser in their 2451<code>userData</code> arguments. The user data can still be obtained 2452using the <code><a href= "#XML_GetUserData" 2453>XML_GetUserData</a></code> function. 2454</div> 2455 2456<h4 id="XML_SetBase">XML_SetBase</h4> 2457<pre class="fcndec"> 2458enum XML_Status XMLCALL 2459XML_SetBase(XML_Parser p, 2460 const XML_Char *base); 2461</pre> 2462<div class="fcndef"> 2463Set the base to be used for resolving relative URIs in system 2464identifiers. The return value is <code>XML_STATUS_ERROR</code> if 2465there's no memory to store base, otherwise it's 2466<code>XML_STATUS_OK</code>. 2467</div> 2468 2469<h4 id="XML_GetBase">XML_GetBase</h4> 2470<pre class="fcndec"> 2471const XML_Char * XMLCALL 2472XML_GetBase(XML_Parser p); 2473</pre> 2474<div class="fcndef"> 2475Return the base for resolving relative URIs. 2476</div> 2477 2478<h4 id="XML_GetSpecifiedAttributeCount">XML_GetSpecifiedAttributeCount</h4> 2479<pre class="fcndec"> 2480int XMLCALL 2481XML_GetSpecifiedAttributeCount(XML_Parser p); 2482</pre> 2483<div class="fcndef"> 2484When attributes are reported to the start handler in the atts vector, 2485attributes that were explicitly set in the element occur before any 2486attributes that receive their value from default information in an 2487ATTLIST declaration. This function returns the number of attributes 2488that were explicitly set times two, thus giving the offset in the 2489<code>atts</code> array passed to the start tag handler of the first 2490attribute set due to defaults. It supplies information for the last 2491call to a start handler. If called inside a start handler, then that 2492means the current call. 2493</div> 2494 2495<h4 id="XML_GetIdAttributeIndex">XML_GetIdAttributeIndex</h4> 2496<pre class="fcndec"> 2497int XMLCALL 2498XML_GetIdAttributeIndex(XML_Parser p); 2499</pre> 2500<div class="fcndef"> 2501Returns the index of the ID attribute passed in the atts array in the 2502last call to <code><a href= "#XML_StartElementHandler" 2503>XML_StartElementHandler</a></code>, or -1 if there is no ID 2504attribute. If called inside a start handler, then that means the 2505current call. 2506</div> 2507 2508<h4 id="XML_GetAttributeInfo">XML_GetAttributeInfo</h4> 2509<pre class="fcndec"> 2510const XML_AttrInfo * XMLCALL 2511XML_GetAttributeInfo(XML_Parser parser); 2512</pre> 2513<pre class="signature"> 2514typedef struct { 2515 XML_Index nameStart; /* Offset to beginning of the attribute name. */ 2516 XML_Index nameEnd; /* Offset after the attribute name's last byte. */ 2517 XML_Index valueStart; /* Offset to beginning of the attribute value. */ 2518 XML_Index valueEnd; /* Offset after the attribute value's last byte. */ 2519} XML_AttrInfo; 2520</pre> 2521<div class="fcndef"> 2522Returns an array of <code>XML_AttrInfo</code> structures for the 2523attribute/value pairs passed in the last call to the 2524<code>XML_StartElementHandler</code> that were specified 2525in the start-tag rather than defaulted. Each attribute/value pair counts 2526as 1; thus the number of entries in the array is 2527<code>XML_GetSpecifiedAttributeCount(parser) / 2</code>. 2528</div> 2529 2530<h4 id="XML_SetEncoding">XML_SetEncoding</h4> 2531<pre class="fcndec"> 2532enum XML_Status XMLCALL 2533XML_SetEncoding(XML_Parser p, 2534 const XML_Char *encoding); 2535</pre> 2536<div class="fcndef"> 2537Set the encoding to be used by the parser. It is equivalent to 2538passing a non-<code>NULL</code> encoding argument to the parser creation functions. 2539It must not be called after <code><a href= "#XML_Parse" 2540>XML_Parse</a></code> or <code><a href= "#XML_ParseBuffer" 2541>XML_ParseBuffer</a></code> have been called on the given parser. 2542Returns <code>XML_STATUS_OK</code> on success or 2543<code>XML_STATUS_ERROR</code> on error. 2544</div> 2545 2546<h4 id="XML_SetParamEntityParsing">XML_SetParamEntityParsing</h4> 2547<pre class="fcndec"> 2548int XMLCALL 2549XML_SetParamEntityParsing(XML_Parser p, 2550 enum XML_ParamEntityParsing code); 2551</pre> 2552<div class="fcndef"> 2553This enables parsing of parameter entities, including the external 2554parameter entity that is the external DTD subset, according to 2555<code>code</code>. 2556The choices for <code>code</code> are: 2557<ul> 2558<li><code>XML_PARAM_ENTITY_PARSING_NEVER</code></li> 2559<li><code>XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE</code></li> 2560<li><code>XML_PARAM_ENTITY_PARSING_ALWAYS</code></li> 2561</ul> 2562<b>Note:</b> If <code>XML_SetParamEntityParsing</code> is called after 2563<code>XML_Parse</code> or <code>XML_ParseBuffer</code>, then it has 2564no effect and will always return 0. 2565</div> 2566 2567<h4 id="XML_SetHashSalt">XML_SetHashSalt</h4> 2568<pre class="fcndec"> 2569int XMLCALL 2570XML_SetHashSalt(XML_Parser p, 2571 unsigned long hash_salt); 2572</pre> 2573<div class="fcndef"> 2574Sets the hash salt to use for internal hash calculations. 2575Helps in preventing DoS attacks based on predicting hash 2576function behavior. In order to have an effect this must be called 2577before parsing has started. Returns 1 if successful, 0 when called 2578after <code>XML_Parse</code> or <code>XML_ParseBuffer</code>. 2579<p><b>Note:</b> This call is optional, as the parser will auto-generate 2580a new random salt value if no value has been set at the start of parsing.</p> 2581<p><b>Note:</b> One should not call <code>XML_SetHashSalt</code> with a 2582hash salt value of 0, as this value is used as sentinel value to indicate 2583that <code>XML_SetHashSalt</code> has <b>not</b> been called. Consequently 2584such a call will have no effect, even if it returns 1.</p> 2585</div> 2586 2587<h4 id="XML_UseForeignDTD">XML_UseForeignDTD</h4> 2588<pre class="fcndec"> 2589enum XML_Error XMLCALL 2590XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD); 2591</pre> 2592<div class="fcndef"> 2593<p>This function allows an application to provide an external subset 2594for the document type declaration for documents which do not specify 2595an external subset of their own. For documents which specify an 2596external subset in their DOCTYPE declaration, the application-provided 2597subset will be ignored. If the document does not contain a DOCTYPE 2598declaration at all and <code>useDTD</code> is true, the 2599application-provided subset will be parsed, but the 2600<code>startDoctypeDeclHandler</code> and 2601<code>endDoctypeDeclHandler</code> functions, if set, will not be 2602called. The setting of parameter entity parsing, controlled using 2603<code><a href= "#XML_SetParamEntityParsing" 2604>XML_SetParamEntityParsing</a></code>, will be honored.</p> 2605 2606<p>The application-provided external subset is read by calling the 2607external entity reference handler set via <code><a href= 2608"#XML_SetExternalEntityRefHandler" 2609>XML_SetExternalEntityRefHandler</a></code> with both 2610<code>publicId</code> and <code>systemId</code> set to <code>NULL</code>.</p> 2611 2612<p>If this function is called after parsing has begun, it returns 2613<code>XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING</code> and ignores 2614<code>useDTD</code>. If called when Expat has been compiled without 2615DTD support, it returns 2616<code>XML_ERROR_FEATURE_REQUIRES_XML_DTD</code>. Otherwise, it 2617returns <code>XML_ERROR_NONE</code>.</p> 2618 2619<p><b>Note:</b> For the purpose of checking WFC: Entity Declared, passing 2620<code>useDTD == XML_TRUE</code> will make the parser behave as if 2621the document had a DTD with an external subset. This holds true even if 2622the external entity reference handler returns without action.</p> 2623</div> 2624 2625<h4 id="XML_SetReturnNSTriplet">XML_SetReturnNSTriplet</h4> 2626<pre class="fcndec"> 2627void XMLCALL 2628XML_SetReturnNSTriplet(XML_Parser parser, 2629 int do_nst); 2630</pre> 2631<div class="fcndef"> 2632<p> 2633This function only has an effect when using a parser created with 2634<code><a href= "#XML_ParserCreateNS" >XML_ParserCreateNS</a></code>, 2635i.e. when namespace processing is in effect. The <code>do_nst</code> 2636sets whether or not prefixes are returned with names qualified with a 2637namespace prefix. If this function is called with <code>do_nst</code> 2638non-zero, then afterwards namespace qualified names (that is qualified 2639with a prefix as opposed to belonging to a default namespace) are 2640returned as a triplet with the three parts separated by the namespace 2641separator specified when the parser was created. The order of 2642returned parts is URI, local name, and prefix.</p> <p>If 2643<code>do_nst</code> is zero, then namespaces are reported in the 2644default manner, URI then local_name separated by the namespace 2645separator.</p> 2646</div> 2647 2648<h4 id="XML_DefaultCurrent">XML_DefaultCurrent</h4> 2649<pre class="fcndec"> 2650void XMLCALL 2651XML_DefaultCurrent(XML_Parser parser); 2652</pre> 2653<div class="fcndef"> 2654This can be called within a handler for a start element, end element, 2655processing instruction or character data. It causes the corresponding 2656markup to be passed to the default handler set by <code><a 2657href="#XML_SetDefaultHandler" >XML_SetDefaultHandler</a></code> or 2658<code><a href="#XML_SetDefaultHandlerExpand" 2659>XML_SetDefaultHandlerExpand</a></code>. It does nothing if there is 2660not a default handler. 2661</div> 2662 2663<h4 id="XML_ExpatVersion">XML_ExpatVersion</h4> 2664<pre class="fcndec"> 2665XML_LChar * XMLCALL 2666XML_ExpatVersion(); 2667</pre> 2668<div class="fcndef"> 2669Return the library version as a string (e.g. <code>"expat_1.95.1"</code>). 2670</div> 2671 2672<h4 id="XML_ExpatVersionInfo">XML_ExpatVersionInfo</h4> 2673<pre class="fcndec"> 2674struct XML_Expat_Version XMLCALL 2675XML_ExpatVersionInfo(); 2676</pre> 2677<pre class="signature"> 2678typedef struct { 2679 int major; 2680 int minor; 2681 int micro; 2682} XML_Expat_Version; 2683</pre> 2684<div class="fcndef"> 2685Return the library version information as a structure. 2686Some macros are also defined that support compile-time tests of the 2687library version: 2688<ul> 2689<li><code>XML_MAJOR_VERSION</code></li> 2690<li><code>XML_MINOR_VERSION</code></li> 2691<li><code>XML_MICRO_VERSION</code></li> 2692</ul> 2693Testing these constants is currently the best way to determine if 2694particular parts of the Expat API are available. 2695</div> 2696 2697<h4 id="XML_GetFeatureList">XML_GetFeatureList</h4> 2698<pre class="fcndec"> 2699const XML_Feature * XMLCALL 2700XML_GetFeatureList(); 2701</pre> 2702<pre class="signature"> 2703enum XML_FeatureEnum { 2704 XML_FEATURE_END = 0, 2705 XML_FEATURE_UNICODE, 2706 XML_FEATURE_UNICODE_WCHAR_T, 2707 XML_FEATURE_DTD, 2708 XML_FEATURE_CONTEXT_BYTES, 2709 XML_FEATURE_MIN_SIZE, 2710 XML_FEATURE_SIZEOF_XML_CHAR, 2711 XML_FEATURE_SIZEOF_XML_LCHAR, 2712 XML_FEATURE_NS, 2713 XML_FEATURE_LARGE_SIZE 2714}; 2715 2716typedef struct { 2717 enum XML_FeatureEnum feature; 2718 XML_LChar *name; 2719 long int value; 2720} XML_Feature; 2721</pre> 2722<div class="fcndef"> 2723<p>Returns a list of "feature" records, providing details on how 2724Expat was configured at compile time. Most applications should not 2725need to worry about this, but this information is otherwise not 2726available from Expat. This function allows code that does need to 2727check these features to do so at runtime.</p> 2728 2729<p>The return value is an array of <code>XML_Feature</code>, 2730terminated by a record with a <code>feature</code> of 2731<code>XML_FEATURE_END</code> and <code>name</code> of <code>NULL</code>, 2732identifying the feature-test macros Expat was compiled with. Since an 2733application that requires this kind of information needs to determine 2734the type of character the <code>name</code> points to, records for the 2735<code>XML_FEATURE_SIZEOF_XML_CHAR</code> and 2736<code>XML_FEATURE_SIZEOF_XML_LCHAR</code> will be located at the 2737beginning of the list, followed by <code>XML_FEATURE_UNICODE</code> 2738and <code>XML_FEATURE_UNICODE_WCHAR_T</code>, if they are present at 2739all.</p> 2740 2741<p>Some features have an associated value. If there isn't an 2742associated value, the <code>value</code> field is set to 0. At this 2743time, the following features have been defined to have values:</p> 2744 2745<dl> 2746 <dt><code>XML_FEATURE_SIZEOF_XML_CHAR</code></dt> 2747 <dd>The number of bytes occupied by one <code>XML_Char</code> 2748 character.</dd> 2749 <dt><code>XML_FEATURE_SIZEOF_XML_LCHAR</code></dt> 2750 <dd>The number of bytes occupied by one <code>XML_LChar</code> 2751 character.</dd> 2752 <dt><code>XML_FEATURE_CONTEXT_BYTES</code></dt> 2753 <dd>The maximum number of characters of context which can be 2754 reported by <code><a href= "#XML_GetInputContext" 2755 >XML_GetInputContext</a></code>.</dd> 2756</dl> 2757</div> 2758 2759<h4 id="XML_FreeContentModel">XML_FreeContentModel</h4> 2760<pre class="fcndec"> 2761void XMLCALL 2762XML_FreeContentModel(XML_Parser parser, XML_Content *model); 2763</pre> 2764<div class="fcndef"> 2765Function to deallocate the <code>model</code> argument passed to the 2766<code>XML_ElementDeclHandler</code> callback set using <code><a 2767href="#XML_SetElementDeclHandler" >XML_ElementDeclHandler</a></code>. 2768This function should not be used for any other purpose. 2769</div> 2770 2771<p>The following functions allow external code to share the memory 2772allocator an <code>XML_Parser</code> has been configured to use. This 2773is especially useful for third-party libraries that interact with a 2774parser object created by application code, or heavily layered 2775applications. This can be essential when using dynamically loaded 2776libraries which use different C standard libraries (this can happen on 2777Windows, at least).</p> 2778 2779<h4 id="XML_MemMalloc">XML_MemMalloc</h4> 2780<pre class="fcndec"> 2781void * XMLCALL 2782XML_MemMalloc(XML_Parser parser, size_t size); 2783</pre> 2784<div class="fcndef"> 2785Allocate <code>size</code> bytes of memory using the allocator the 2786<code>parser</code> object has been configured to use. Returns a 2787pointer to the memory or <code>NULL</code> on failure. Memory allocated in this 2788way must be freed using <code><a href="#XML_MemFree" 2789>XML_MemFree</a></code>. 2790</div> 2791 2792<h4 id="XML_MemRealloc">XML_MemRealloc</h4> 2793<pre class="fcndec"> 2794void * XMLCALL 2795XML_MemRealloc(XML_Parser parser, void *ptr, size_t size); 2796</pre> 2797<div class="fcndef"> 2798Allocate <code>size</code> bytes of memory using the allocator the 2799<code>parser</code> object has been configured to use. 2800<code>ptr</code> must point to a block of memory allocated by <code><a 2801href="#XML_MemMalloc" >XML_MemMalloc</a></code> or 2802<code>XML_MemRealloc</code>, or be <code>NULL</code>. This function tries to 2803expand the block pointed to by <code>ptr</code> if possible. Returns 2804a pointer to the memory or <code>NULL</code> on failure. On success, the original 2805block has either been expanded or freed. On failure, the original 2806block has not been freed; the caller is responsible for freeing the 2807original block. Memory allocated in this way must be freed using 2808<code><a href="#XML_MemFree" 2809>XML_MemFree</a></code>. 2810</div> 2811 2812<h4 id="XML_MemFree">XML_MemFree</h4> 2813<pre class="fcndec"> 2814void XMLCALL 2815XML_MemFree(XML_Parser parser, void *ptr); 2816</pre> 2817<div class="fcndef"> 2818Free a block of memory pointed to by <code>ptr</code>. The block must 2819have been allocated by <code><a href="#XML_MemMalloc" 2820>XML_MemMalloc</a></code> or <code>XML_MemRealloc</code>, or be <code>NULL</code>. 2821</div> 2822 2823<hr /> 2824 2825 <div class="footer"> 2826 Found a bug in the documentation? 2827 <a href="https://github.com/libexpat/libexpat/issues">Please file a bug report.</a> 2828 </div> 2829 2830</div> 2831</body> 2832</html> 2833