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