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