xref: /freebsd/contrib/expat/lib/expat.h (revision c5f3a7f62217f20f0c7b2c4fc3fb2646336b0802)
1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10    Copyright (c) 2000      Clark Cooper <coopercc@users.sourceforge.net>
11    Copyright (c) 2000-2005 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
12    Copyright (c) 2001-2002 Greg Stein <gstein@users.sourceforge.net>
13    Copyright (c) 2002-2016 Karl Waclawek <karl@waclawek.net>
14    Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org>
15    Copyright (c) 2016      Cristian Rodríguez <crrodriguez@opensuse.org>
16    Copyright (c) 2016      Thomas Beutlich <tc@tbeu.de>
17    Copyright (c) 2017      Rhodri James <rhodri@wildebeest.org.uk>
18    Copyright (c) 2022      Thijs Schreijer <thijs@thijsschreijer.nl>
19    Copyright (c) 2023      Hanno Böck <hanno@gentoo.org>
20    Copyright (c) 2023      Sony Corporation / Snild Dolkow <snild@sony.com>
21    Copyright (c) 2024      Taichi Haradaguchi <20001722@ymail.ne.jp>
22    Licensed under the MIT license:
23 
24    Permission is  hereby granted,  free of charge,  to any  person obtaining
25    a  copy  of  this  software   and  associated  documentation  files  (the
26    "Software"),  to  deal in  the  Software  without restriction,  including
27    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
28    distribute, sublicense, and/or sell copies of the Software, and to permit
29    persons  to whom  the Software  is  furnished to  do so,  subject to  the
30    following conditions:
31 
32    The above copyright  notice and this permission notice  shall be included
33    in all copies or substantial portions of the Software.
34 
35    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
36    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
37    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
38    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
39    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
40    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
41    USE OR OTHER DEALINGS IN THE SOFTWARE.
42 */
43 
44 #ifndef Expat_INCLUDED
45 #define Expat_INCLUDED 1
46 
47 #include <stdlib.h>
48 #include "expat_external.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 struct XML_ParserStruct;
55 typedef struct XML_ParserStruct *XML_Parser;
56 
57 typedef unsigned char XML_Bool;
58 #define XML_TRUE ((XML_Bool)1)
59 #define XML_FALSE ((XML_Bool)0)
60 
61 /* The XML_Status enum gives the possible return values for several
62    API functions.  The preprocessor #defines are included so this
63    stanza can be added to code that still needs to support older
64    versions of Expat 1.95.x:
65 
66    #ifndef XML_STATUS_OK
67    #define XML_STATUS_OK    1
68    #define XML_STATUS_ERROR 0
69    #endif
70 
71    Otherwise, the #define hackery is quite ugly and would have been
72    dropped.
73 */
74 enum XML_Status {
75   XML_STATUS_ERROR = 0,
76 #define XML_STATUS_ERROR XML_STATUS_ERROR
77   XML_STATUS_OK = 1,
78 #define XML_STATUS_OK XML_STATUS_OK
79   XML_STATUS_SUSPENDED = 2
80 #define XML_STATUS_SUSPENDED XML_STATUS_SUSPENDED
81 };
82 
83 enum XML_Error {
84   XML_ERROR_NONE,
85   XML_ERROR_NO_MEMORY,
86   XML_ERROR_SYNTAX,
87   XML_ERROR_NO_ELEMENTS,
88   XML_ERROR_INVALID_TOKEN,
89   XML_ERROR_UNCLOSED_TOKEN,
90   XML_ERROR_PARTIAL_CHAR,
91   XML_ERROR_TAG_MISMATCH,
92   XML_ERROR_DUPLICATE_ATTRIBUTE,
93   XML_ERROR_JUNK_AFTER_DOC_ELEMENT,
94   XML_ERROR_PARAM_ENTITY_REF,
95   XML_ERROR_UNDEFINED_ENTITY,
96   XML_ERROR_RECURSIVE_ENTITY_REF,
97   XML_ERROR_ASYNC_ENTITY,
98   XML_ERROR_BAD_CHAR_REF,
99   XML_ERROR_BINARY_ENTITY_REF,
100   XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF,
101   XML_ERROR_MISPLACED_XML_PI,
102   XML_ERROR_UNKNOWN_ENCODING,
103   XML_ERROR_INCORRECT_ENCODING,
104   XML_ERROR_UNCLOSED_CDATA_SECTION,
105   XML_ERROR_EXTERNAL_ENTITY_HANDLING,
106   XML_ERROR_NOT_STANDALONE,
107   XML_ERROR_UNEXPECTED_STATE,
108   XML_ERROR_ENTITY_DECLARED_IN_PE,
109   XML_ERROR_FEATURE_REQUIRES_XML_DTD,
110   XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING,
111   /* Added in 1.95.7. */
112   XML_ERROR_UNBOUND_PREFIX,
113   /* Added in 1.95.8. */
114   XML_ERROR_UNDECLARING_PREFIX,
115   XML_ERROR_INCOMPLETE_PE,
116   XML_ERROR_XML_DECL,
117   XML_ERROR_TEXT_DECL,
118   XML_ERROR_PUBLICID,
119   XML_ERROR_SUSPENDED,
120   XML_ERROR_NOT_SUSPENDED,
121   XML_ERROR_ABORTED,
122   XML_ERROR_FINISHED,
123   XML_ERROR_SUSPEND_PE,
124   /* Added in 2.0. */
125   XML_ERROR_RESERVED_PREFIX_XML,
126   XML_ERROR_RESERVED_PREFIX_XMLNS,
127   XML_ERROR_RESERVED_NAMESPACE_URI,
128   /* Added in 2.2.1. */
129   XML_ERROR_INVALID_ARGUMENT,
130   /* Added in 2.3.0. */
131   XML_ERROR_NO_BUFFER,
132   /* Added in 2.4.0. */
133   XML_ERROR_AMPLIFICATION_LIMIT_BREACH
134 };
135 
136 enum XML_Content_Type {
137   XML_CTYPE_EMPTY = 1,
138   XML_CTYPE_ANY,
139   XML_CTYPE_MIXED,
140   XML_CTYPE_NAME,
141   XML_CTYPE_CHOICE,
142   XML_CTYPE_SEQ
143 };
144 
145 enum XML_Content_Quant {
146   XML_CQUANT_NONE,
147   XML_CQUANT_OPT,
148   XML_CQUANT_REP,
149   XML_CQUANT_PLUS
150 };
151 
152 /* If type == XML_CTYPE_EMPTY or XML_CTYPE_ANY, then quant will be
153    XML_CQUANT_NONE, and the other fields will be zero or NULL.
154    If type == XML_CTYPE_MIXED, then quant will be NONE or REP and
155    numchildren will contain number of elements that may be mixed in
156    and children point to an array of XML_Content cells that will be
157    all of XML_CTYPE_NAME type with no quantification.
158 
159    If type == XML_CTYPE_NAME, then the name points to the name, and
160    the numchildren field will be zero and children will be NULL. The
161    quant fields indicates any quantifiers placed on the name.
162 
163    CHOICE and SEQ will have name NULL, the number of children in
164    numchildren and children will point, recursively, to an array
165    of XML_Content cells.
166 
167    The EMPTY, ANY, and MIXED types will only occur at top level.
168 */
169 
170 typedef struct XML_cp XML_Content;
171 
172 struct XML_cp {
173   enum XML_Content_Type type;
174   enum XML_Content_Quant quant;
175   XML_Char *name;
176   unsigned int numchildren;
177   XML_Content *children;
178 };
179 
180 /* This is called for an element declaration. See above for
181    description of the model argument. It's the user code's responsibility
182    to free model when finished with it. See XML_FreeContentModel.
183    There is no need to free the model from the handler, it can be kept
184    around and freed at a later stage.
185 */
186 typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData,
187                                               const XML_Char *name,
188                                               XML_Content *model);
189 
190 XMLPARSEAPI(void)
191 XML_SetElementDeclHandler(XML_Parser parser, XML_ElementDeclHandler eldecl);
192 
193 /* The Attlist declaration handler is called for *each* attribute. So
194    a single Attlist declaration with multiple attributes declared will
195    generate multiple calls to this handler. The "default" parameter
196    may be NULL in the case of the "#IMPLIED" or "#REQUIRED"
197    keyword. The "isrequired" parameter will be true and the default
198    value will be NULL in the case of "#REQUIRED". If "isrequired" is
199    true and default is non-NULL, then this is a "#FIXED" default.
200 */
201 typedef void(XMLCALL *XML_AttlistDeclHandler)(
202     void *userData, const XML_Char *elname, const XML_Char *attname,
203     const XML_Char *att_type, const XML_Char *dflt, int isrequired);
204 
205 XMLPARSEAPI(void)
206 XML_SetAttlistDeclHandler(XML_Parser parser, XML_AttlistDeclHandler attdecl);
207 
208 /* The XML declaration handler is called for *both* XML declarations
209    and text declarations. The way to distinguish is that the version
210    parameter will be NULL for text declarations. The encoding
211    parameter may be NULL for XML declarations. The standalone
212    parameter will be -1, 0, or 1 indicating respectively that there
213    was no standalone parameter in the declaration, that it was given
214    as no, or that it was given as yes.
215 */
216 typedef void(XMLCALL *XML_XmlDeclHandler)(void *userData,
217                                           const XML_Char *version,
218                                           const XML_Char *encoding,
219                                           int standalone);
220 
221 XMLPARSEAPI(void)
222 XML_SetXmlDeclHandler(XML_Parser parser, XML_XmlDeclHandler xmldecl);
223 
224 typedef struct {
225   void *(*malloc_fcn)(size_t size);
226   void *(*realloc_fcn)(void *ptr, size_t size);
227   void (*free_fcn)(void *ptr);
228 } XML_Memory_Handling_Suite;
229 
230 /* Constructs a new parser; encoding is the encoding specified by the
231    external protocol or NULL if there is none specified.
232 */
233 XMLPARSEAPI(XML_Parser)
234 XML_ParserCreate(const XML_Char *encoding);
235 
236 /* Constructs a new parser and namespace processor.  Element type
237    names and attribute names that belong to a namespace will be
238    expanded; unprefixed attribute names are never expanded; unprefixed
239    element type names are expanded only if there is a default
240    namespace. The expanded name is the concatenation of the namespace
241    URI, the namespace separator character, and the local part of the
242    name.  If the namespace separator is '\0' then the namespace URI
243    and the local part will be concatenated without any separator.
244    It is a programming error to use the separator '\0' with namespace
245    triplets (see XML_SetReturnNSTriplet).
246    If a namespace separator is chosen that can be part of a URI or
247    part of an XML name, splitting an expanded name back into its
248    1, 2 or 3 original parts on application level in the element handler
249    may end up vulnerable, so these are advised against;  sane choices for
250    a namespace separator are e.g. '\n' (line feed) and '|' (pipe).
251 
252    Note that Expat does not validate namespace URIs (beyond encoding)
253    against RFC 3986 today (and is not required to do so with regard to
254    the XML 1.0 namespaces specification) but it may start doing that
255    in future releases.  Before that, an application using Expat must
256    be ready to receive namespace URIs containing non-URI characters.
257 */
258 XMLPARSEAPI(XML_Parser)
259 XML_ParserCreateNS(const XML_Char *encoding, XML_Char namespaceSeparator);
260 
261 /* Constructs a new parser using the memory management suite referred to
262    by memsuite. If memsuite is NULL, then use the standard library memory
263    suite. If namespaceSeparator is non-NULL it creates a parser with
264    namespace processing as described above. The character pointed at
265    will serve as the namespace separator.
266 
267    All further memory operations used for the created parser will come from
268    the given suite.
269 */
270 XMLPARSEAPI(XML_Parser)
271 XML_ParserCreate_MM(const XML_Char *encoding,
272                     const XML_Memory_Handling_Suite *memsuite,
273                     const XML_Char *namespaceSeparator);
274 
275 /* Prepare a parser object to be reused.  This is particularly
276    valuable when memory allocation overhead is disproportionately high,
277    such as when a large number of small documnents need to be parsed.
278    All handlers are cleared from the parser, except for the
279    unknownEncodingHandler. The parser's external state is re-initialized
280    except for the values of ns and ns_triplets.
281 
282    Added in Expat 1.95.3.
283 */
284 XMLPARSEAPI(XML_Bool)
285 XML_ParserReset(XML_Parser parser, const XML_Char *encoding);
286 
287 /* atts is array of name/value pairs, terminated by 0;
288    names and values are 0 terminated.
289 */
290 typedef void(XMLCALL *XML_StartElementHandler)(void *userData,
291                                                const XML_Char *name,
292                                                const XML_Char **atts);
293 
294 typedef void(XMLCALL *XML_EndElementHandler)(void *userData,
295                                              const XML_Char *name);
296 
297 /* s is not 0 terminated. */
298 typedef void(XMLCALL *XML_CharacterDataHandler)(void *userData,
299                                                 const XML_Char *s, int len);
300 
301 /* target and data are 0 terminated */
302 typedef void(XMLCALL *XML_ProcessingInstructionHandler)(void *userData,
303                                                         const XML_Char *target,
304                                                         const XML_Char *data);
305 
306 /* data is 0 terminated */
307 typedef void(XMLCALL *XML_CommentHandler)(void *userData, const XML_Char *data);
308 
309 typedef void(XMLCALL *XML_StartCdataSectionHandler)(void *userData);
310 typedef void(XMLCALL *XML_EndCdataSectionHandler)(void *userData);
311 
312 /* This is called for any characters in the XML document for which
313    there is no applicable handler.  This includes both characters that
314    are part of markup which is of a kind that is not reported
315    (comments, markup declarations), or characters that are part of a
316    construct which could be reported but for which no handler has been
317    supplied. The characters are passed exactly as they were in the XML
318    document except that they will be encoded in UTF-8 or UTF-16.
319    Line boundaries are not normalized. Note that a byte order mark
320    character is not passed to the default handler. There are no
321    guarantees about how characters are divided between calls to the
322    default handler: for example, a comment might be split between
323    multiple calls.
324 */
325 typedef void(XMLCALL *XML_DefaultHandler)(void *userData, const XML_Char *s,
326                                           int len);
327 
328 /* This is called for the start of the DOCTYPE declaration, before
329    any DTD or internal subset is parsed.
330 */
331 typedef void(XMLCALL *XML_StartDoctypeDeclHandler)(void *userData,
332                                                    const XML_Char *doctypeName,
333                                                    const XML_Char *sysid,
334                                                    const XML_Char *pubid,
335                                                    int has_internal_subset);
336 
337 /* This is called for the end of the DOCTYPE declaration when the
338    closing > is encountered, but after processing any external
339    subset.
340 */
341 typedef void(XMLCALL *XML_EndDoctypeDeclHandler)(void *userData);
342 
343 /* This is called for entity declarations. The is_parameter_entity
344    argument will be non-zero if the entity is a parameter entity, zero
345    otherwise.
346 
347    For internal entities (<!ENTITY foo "bar">), value will
348    be non-NULL and systemId, publicID, and notationName will be NULL.
349    The value string is NOT null-terminated; the length is provided in
350    the value_length argument. Since it is legal to have zero-length
351    values, do not use this argument to test for internal entities.
352 
353    For external entities, value will be NULL and systemId will be
354    non-NULL. The publicId argument will be NULL unless a public
355    identifier was provided. The notationName argument will have a
356    non-NULL value only for unparsed entity declarations.
357 
358    Note that is_parameter_entity can't be changed to XML_Bool, since
359    that would break binary compatibility.
360 */
361 typedef void(XMLCALL *XML_EntityDeclHandler)(
362     void *userData, const XML_Char *entityName, int is_parameter_entity,
363     const XML_Char *value, int value_length, const XML_Char *base,
364     const XML_Char *systemId, const XML_Char *publicId,
365     const XML_Char *notationName);
366 
367 XMLPARSEAPI(void)
368 XML_SetEntityDeclHandler(XML_Parser parser, XML_EntityDeclHandler handler);
369 
370 /* OBSOLETE -- OBSOLETE -- OBSOLETE
371    This handler has been superseded by the EntityDeclHandler above.
372    It is provided here for backward compatibility.
373 
374    This is called for a declaration of an unparsed (NDATA) entity.
375    The base argument is whatever was set by XML_SetBase. The
376    entityName, systemId and notationName arguments will never be
377    NULL. The other arguments may be.
378 */
379 typedef void(XMLCALL *XML_UnparsedEntityDeclHandler)(
380     void *userData, const XML_Char *entityName, const XML_Char *base,
381     const XML_Char *systemId, const XML_Char *publicId,
382     const XML_Char *notationName);
383 
384 /* This is called for a declaration of notation.  The base argument is
385    whatever was set by XML_SetBase. The notationName will never be
386    NULL.  The other arguments can be.
387 */
388 typedef void(XMLCALL *XML_NotationDeclHandler)(void *userData,
389                                                const XML_Char *notationName,
390                                                const XML_Char *base,
391                                                const XML_Char *systemId,
392                                                const XML_Char *publicId);
393 
394 /* When namespace processing is enabled, these are called once for
395    each namespace declaration. The call to the start and end element
396    handlers occur between the calls to the start and end namespace
397    declaration handlers. For an xmlns attribute, prefix will be
398    NULL.  For an xmlns="" attribute, uri will be NULL.
399 */
400 typedef void(XMLCALL *XML_StartNamespaceDeclHandler)(void *userData,
401                                                      const XML_Char *prefix,
402                                                      const XML_Char *uri);
403 
404 typedef void(XMLCALL *XML_EndNamespaceDeclHandler)(void *userData,
405                                                    const XML_Char *prefix);
406 
407 /* This is called if the document is not standalone, that is, it has an
408    external subset or a reference to a parameter entity, but does not
409    have standalone="yes". If this handler returns XML_STATUS_ERROR,
410    then processing will not continue, and the parser will return a
411    XML_ERROR_NOT_STANDALONE error.
412    If parameter entity parsing is enabled, then in addition to the
413    conditions above this handler will only be called if the referenced
414    entity was actually read.
415 */
416 typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData);
417 
418 /* This is called for a reference to an external parsed general
419    entity.  The referenced entity is not automatically parsed.  The
420    application can parse it immediately or later using
421    XML_ExternalEntityParserCreate.
422 
423    The parser argument is the parser parsing the entity containing the
424    reference; it can be passed as the parser argument to
425    XML_ExternalEntityParserCreate.  The systemId argument is the
426    system identifier as specified in the entity declaration; it will
427    not be NULL.
428 
429    The base argument is the system identifier that should be used as
430    the base for resolving systemId if systemId was relative; this is
431    set by XML_SetBase; it may be NULL.
432 
433    The publicId argument is the public identifier as specified in the
434    entity declaration, or NULL if none was specified; the whitespace
435    in the public identifier will have been normalized as required by
436    the XML spec.
437 
438    The context argument specifies the parsing context in the format
439    expected by the context argument to XML_ExternalEntityParserCreate;
440    context is valid only until the handler returns, so if the
441    referenced entity is to be parsed later, it must be copied.
442    context is NULL only when the entity is a parameter entity.
443 
444    The handler should return XML_STATUS_ERROR if processing should not
445    continue because of a fatal error in the handling of the external
446    entity.  In this case the calling parser will return an
447    XML_ERROR_EXTERNAL_ENTITY_HANDLING error.
448 
449    Note that unlike other handlers the first argument is the parser,
450    not userData.
451 */
452 typedef int(XMLCALL *XML_ExternalEntityRefHandler)(XML_Parser parser,
453                                                    const XML_Char *context,
454                                                    const XML_Char *base,
455                                                    const XML_Char *systemId,
456                                                    const XML_Char *publicId);
457 
458 /* This is called in two situations:
459    1) An entity reference is encountered for which no declaration
460       has been read *and* this is not an error.
461    2) An internal entity reference is read, but not expanded, because
462       XML_SetDefaultHandler has been called.
463    Note: skipped parameter entities in declarations and skipped general
464          entities in attribute values cannot be reported, because
465          the event would be out of sync with the reporting of the
466          declarations or attribute values
467 */
468 typedef void(XMLCALL *XML_SkippedEntityHandler)(void *userData,
469                                                 const XML_Char *entityName,
470                                                 int is_parameter_entity);
471 
472 /* This structure is filled in by the XML_UnknownEncodingHandler to
473    provide information to the parser about encodings that are unknown
474    to the parser.
475 
476    The map[b] member gives information about byte sequences whose
477    first byte is b.
478 
479    If map[b] is c where c is >= 0, then b by itself encodes the
480    Unicode scalar value c.
481 
482    If map[b] is -1, then the byte sequence is malformed.
483 
484    If map[b] is -n, where n >= 2, then b is the first byte of an
485    n-byte sequence that encodes a single Unicode scalar value.
486 
487    The data member will be passed as the first argument to the convert
488    function.
489 
490    The convert function is used to convert multibyte sequences; s will
491    point to a n-byte sequence where map[(unsigned char)*s] == -n.  The
492    convert function must return the Unicode scalar value represented
493    by this byte sequence or -1 if the byte sequence is malformed.
494 
495    The convert function may be NULL if the encoding is a single-byte
496    encoding, that is if map[b] >= -1 for all bytes b.
497 
498    When the parser is finished with the encoding, then if release is
499    not NULL, it will call release passing it the data member; once
500    release has been called, the convert function will not be called
501    again.
502 
503    Expat places certain restrictions on the encodings that are supported
504    using this mechanism.
505 
506    1. Every ASCII character that can appear in a well-formed XML document,
507       other than the characters
508 
509       $@\^`{}~
510 
511       must be represented by a single byte, and that byte must be the
512       same byte that represents that character in ASCII.
513 
514    2. No character may require more than 4 bytes to encode.
515 
516    3. All characters encoded must have Unicode scalar values <=
517       0xFFFF, (i.e., characters that would be encoded by surrogates in
518       UTF-16 are  not allowed).  Note that this restriction doesn't
519       apply to the built-in support for UTF-8 and UTF-16.
520 
521    4. No Unicode character may be encoded by more than one distinct
522       sequence of bytes.
523 */
524 typedef struct {
525   int map[256];
526   void *data;
527   int(XMLCALL *convert)(void *data, const char *s);
528   void(XMLCALL *release)(void *data);
529 } XML_Encoding;
530 
531 /* This is called for an encoding that is unknown to the parser.
532 
533    The encodingHandlerData argument is that which was passed as the
534    second argument to XML_SetUnknownEncodingHandler.
535 
536    The name argument gives the name of the encoding as specified in
537    the encoding declaration.
538 
539    If the callback can provide information about the encoding, it must
540    fill in the XML_Encoding structure, and return XML_STATUS_OK.
541    Otherwise it must return XML_STATUS_ERROR.
542 
543    If info does not describe a suitable encoding, then the parser will
544    return an XML_ERROR_UNKNOWN_ENCODING error.
545 */
546 typedef int(XMLCALL *XML_UnknownEncodingHandler)(void *encodingHandlerData,
547                                                  const XML_Char *name,
548                                                  XML_Encoding *info);
549 
550 XMLPARSEAPI(void)
551 XML_SetElementHandler(XML_Parser parser, XML_StartElementHandler start,
552                       XML_EndElementHandler end);
553 
554 XMLPARSEAPI(void)
555 XML_SetStartElementHandler(XML_Parser parser, XML_StartElementHandler handler);
556 
557 XMLPARSEAPI(void)
558 XML_SetEndElementHandler(XML_Parser parser, XML_EndElementHandler handler);
559 
560 XMLPARSEAPI(void)
561 XML_SetCharacterDataHandler(XML_Parser parser,
562                             XML_CharacterDataHandler handler);
563 
564 XMLPARSEAPI(void)
565 XML_SetProcessingInstructionHandler(XML_Parser parser,
566                                     XML_ProcessingInstructionHandler handler);
567 XMLPARSEAPI(void)
568 XML_SetCommentHandler(XML_Parser parser, XML_CommentHandler handler);
569 
570 XMLPARSEAPI(void)
571 XML_SetCdataSectionHandler(XML_Parser parser,
572                            XML_StartCdataSectionHandler start,
573                            XML_EndCdataSectionHandler end);
574 
575 XMLPARSEAPI(void)
576 XML_SetStartCdataSectionHandler(XML_Parser parser,
577                                 XML_StartCdataSectionHandler start);
578 
579 XMLPARSEAPI(void)
580 XML_SetEndCdataSectionHandler(XML_Parser parser,
581                               XML_EndCdataSectionHandler end);
582 
583 /* This sets the default handler and also inhibits expansion of
584    internal entities. These entity references will be passed to the
585    default handler, or to the skipped entity handler, if one is set.
586 */
587 XMLPARSEAPI(void)
588 XML_SetDefaultHandler(XML_Parser parser, XML_DefaultHandler handler);
589 
590 /* This sets the default handler but does not inhibit expansion of
591    internal entities.  The entity reference will not be passed to the
592    default handler.
593 */
594 XMLPARSEAPI(void)
595 XML_SetDefaultHandlerExpand(XML_Parser parser, XML_DefaultHandler handler);
596 
597 XMLPARSEAPI(void)
598 XML_SetDoctypeDeclHandler(XML_Parser parser, XML_StartDoctypeDeclHandler start,
599                           XML_EndDoctypeDeclHandler end);
600 
601 XMLPARSEAPI(void)
602 XML_SetStartDoctypeDeclHandler(XML_Parser parser,
603                                XML_StartDoctypeDeclHandler start);
604 
605 XMLPARSEAPI(void)
606 XML_SetEndDoctypeDeclHandler(XML_Parser parser, XML_EndDoctypeDeclHandler end);
607 
608 XMLPARSEAPI(void)
609 XML_SetUnparsedEntityDeclHandler(XML_Parser parser,
610                                  XML_UnparsedEntityDeclHandler handler);
611 
612 XMLPARSEAPI(void)
613 XML_SetNotationDeclHandler(XML_Parser parser, XML_NotationDeclHandler handler);
614 
615 XMLPARSEAPI(void)
616 XML_SetNamespaceDeclHandler(XML_Parser parser,
617                             XML_StartNamespaceDeclHandler start,
618                             XML_EndNamespaceDeclHandler end);
619 
620 XMLPARSEAPI(void)
621 XML_SetStartNamespaceDeclHandler(XML_Parser parser,
622                                  XML_StartNamespaceDeclHandler start);
623 
624 XMLPARSEAPI(void)
625 XML_SetEndNamespaceDeclHandler(XML_Parser parser,
626                                XML_EndNamespaceDeclHandler end);
627 
628 XMLPARSEAPI(void)
629 XML_SetNotStandaloneHandler(XML_Parser parser,
630                             XML_NotStandaloneHandler handler);
631 
632 XMLPARSEAPI(void)
633 XML_SetExternalEntityRefHandler(XML_Parser parser,
634                                 XML_ExternalEntityRefHandler handler);
635 
636 /* If a non-NULL value for arg is specified here, then it will be
637    passed as the first argument to the external entity ref handler
638    instead of the parser object.
639 */
640 XMLPARSEAPI(void)
641 XML_SetExternalEntityRefHandlerArg(XML_Parser parser, void *arg);
642 
643 XMLPARSEAPI(void)
644 XML_SetSkippedEntityHandler(XML_Parser parser,
645                             XML_SkippedEntityHandler handler);
646 
647 XMLPARSEAPI(void)
648 XML_SetUnknownEncodingHandler(XML_Parser parser,
649                               XML_UnknownEncodingHandler handler,
650                               void *encodingHandlerData);
651 
652 /* This can be called within a handler for a start element, end
653    element, processing instruction or character data.  It causes the
654    corresponding markup to be passed to the default handler.
655 */
656 XMLPARSEAPI(void)
657 XML_DefaultCurrent(XML_Parser parser);
658 
659 /* If do_nst is non-zero, and namespace processing is in effect, and
660    a name has a prefix (i.e. an explicit namespace qualifier) then
661    that name is returned as a triplet in a single string separated by
662    the separator character specified when the parser was created: URI
663    + sep + local_name + sep + prefix.
664 
665    If do_nst is zero, then namespace information is returned in the
666    default manner (URI + sep + local_name) whether or not the name
667    has a prefix.
668 
669    Note: Calling XML_SetReturnNSTriplet after XML_Parse or
670      XML_ParseBuffer has no effect.
671 */
672 
673 XMLPARSEAPI(void)
674 XML_SetReturnNSTriplet(XML_Parser parser, int do_nst);
675 
676 /* This value is passed as the userData argument to callbacks. */
677 XMLPARSEAPI(void)
678 XML_SetUserData(XML_Parser parser, void *userData);
679 
680 /* Returns the last value set by XML_SetUserData or NULL. */
681 #define XML_GetUserData(parser) (*(void **)(parser))
682 
683 /* This is equivalent to supplying an encoding argument to
684    XML_ParserCreate. On success XML_SetEncoding returns non-zero,
685    zero otherwise.
686    Note: Calling XML_SetEncoding after XML_Parse or XML_ParseBuffer
687      has no effect and returns XML_STATUS_ERROR.
688 */
689 XMLPARSEAPI(enum XML_Status)
690 XML_SetEncoding(XML_Parser parser, const XML_Char *encoding);
691 
692 /* If this function is called, then the parser will be passed as the
693    first argument to callbacks instead of userData.  The userData will
694    still be accessible using XML_GetUserData.
695 */
696 XMLPARSEAPI(void)
697 XML_UseParserAsHandlerArg(XML_Parser parser);
698 
699 /* If useDTD == XML_TRUE is passed to this function, then the parser
700    will assume that there is an external subset, even if none is
701    specified in the document. In such a case the parser will call the
702    externalEntityRefHandler with a value of NULL for the systemId
703    argument (the publicId and context arguments will be NULL as well).
704    Note: For the purpose of checking WFC: Entity Declared, passing
705      useDTD == XML_TRUE will make the parser behave as if the document
706      had a DTD with an external subset.
707    Note: If this function is called, then this must be done before
708      the first call to XML_Parse or XML_ParseBuffer, since it will
709      have no effect after that.  Returns
710      XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING.
711    Note: If the document does not have a DOCTYPE declaration at all,
712      then startDoctypeDeclHandler and endDoctypeDeclHandler will not
713      be called, despite an external subset being parsed.
714    Note: If XML_DTD is not defined when Expat is compiled, returns
715      XML_ERROR_FEATURE_REQUIRES_XML_DTD.
716    Note: If parser == NULL, returns XML_ERROR_INVALID_ARGUMENT.
717 */
718 XMLPARSEAPI(enum XML_Error)
719 XML_UseForeignDTD(XML_Parser parser, XML_Bool useDTD);
720 
721 /* Sets the base to be used for resolving relative URIs in system
722    identifiers in declarations.  Resolving relative identifiers is
723    left to the application: this value will be passed through as the
724    base argument to the XML_ExternalEntityRefHandler,
725    XML_NotationDeclHandler and XML_UnparsedEntityDeclHandler. The base
726    argument will be copied.  Returns XML_STATUS_ERROR if out of memory,
727    XML_STATUS_OK otherwise.
728 */
729 XMLPARSEAPI(enum XML_Status)
730 XML_SetBase(XML_Parser parser, const XML_Char *base);
731 
732 XMLPARSEAPI(const XML_Char *)
733 XML_GetBase(XML_Parser parser);
734 
735 /* Returns the number of the attribute/value pairs passed in last call
736    to the XML_StartElementHandler that were specified in the start-tag
737    rather than defaulted. Each attribute/value pair counts as 2; thus
738    this corresponds to an index into the atts array passed to the
739    XML_StartElementHandler.  Returns -1 if parser == NULL.
740 */
741 XMLPARSEAPI(int)
742 XML_GetSpecifiedAttributeCount(XML_Parser parser);
743 
744 /* Returns the index of the ID attribute passed in the last call to
745    XML_StartElementHandler, or -1 if there is no ID attribute or
746    parser == NULL.  Each attribute/value pair counts as 2; thus this
747    corresponds to an index into the atts array passed to the
748    XML_StartElementHandler.
749 */
750 XMLPARSEAPI(int)
751 XML_GetIdAttributeIndex(XML_Parser parser);
752 
753 #ifdef XML_ATTR_INFO
754 /* Source file byte offsets for the start and end of attribute names and values.
755    The value indices are exclusive of surrounding quotes; thus in a UTF-8 source
756    file an attribute value of "blah" will yield:
757    info->valueEnd - info->valueStart = 4 bytes.
758 */
759 typedef struct {
760   XML_Index nameStart;  /* Offset to beginning of the attribute name. */
761   XML_Index nameEnd;    /* Offset after the attribute name's last byte. */
762   XML_Index valueStart; /* Offset to beginning of the attribute value. */
763   XML_Index valueEnd;   /* Offset after the attribute value's last byte. */
764 } XML_AttrInfo;
765 
766 /* Returns an array of XML_AttrInfo structures for the attribute/value pairs
767    passed in last call to the XML_StartElementHandler that were specified
768    in the start-tag rather than defaulted. Each attribute/value pair counts
769    as 1; thus the number of entries in the array is
770    XML_GetSpecifiedAttributeCount(parser) / 2.
771 */
772 XMLPARSEAPI(const XML_AttrInfo *)
773 XML_GetAttributeInfo(XML_Parser parser);
774 #endif
775 
776 /* Parses some input. Returns XML_STATUS_ERROR if a fatal error is
777    detected.  The last call to XML_Parse must have isFinal true; len
778    may be zero for this call (or any other).
779 
780    Though the return values for these functions has always been
781    described as a Boolean value, the implementation, at least for the
782    1.95.x series, has always returned exactly one of the XML_Status
783    values.
784 */
785 XMLPARSEAPI(enum XML_Status)
786 XML_Parse(XML_Parser parser, const char *s, int len, int isFinal);
787 
788 XMLPARSEAPI(void *)
789 XML_GetBuffer(XML_Parser parser, int len);
790 
791 XMLPARSEAPI(enum XML_Status)
792 XML_ParseBuffer(XML_Parser parser, int len, int isFinal);
793 
794 /* Stops parsing, causing XML_Parse() or XML_ParseBuffer() to return.
795    Must be called from within a call-back handler, except when aborting
796    (resumable = 0) an already suspended parser. Some call-backs may
797    still follow because they would otherwise get lost. Examples:
798    - endElementHandler() for empty elements when stopped in
799      startElementHandler(),
800    - endNameSpaceDeclHandler() when stopped in endElementHandler(),
801    and possibly others.
802 
803    Can be called from most handlers, including DTD related call-backs,
804    except when parsing an external parameter entity and resumable != 0.
805    Returns XML_STATUS_OK when successful, XML_STATUS_ERROR otherwise.
806    Possible error codes:
807    - XML_ERROR_SUSPENDED: when suspending an already suspended parser.
808    - XML_ERROR_FINISHED: when the parser has already finished.
809    - XML_ERROR_SUSPEND_PE: when suspending while parsing an external PE.
810 
811    When resumable != 0 (true) then parsing is suspended, that is,
812    XML_Parse() and XML_ParseBuffer() return XML_STATUS_SUSPENDED.
813    Otherwise, parsing is aborted, that is, XML_Parse() and XML_ParseBuffer()
814    return XML_STATUS_ERROR with error code XML_ERROR_ABORTED.
815 
816    *Note*:
817    This will be applied to the current parser instance only, that is, if
818    there is a parent parser then it will continue parsing when the
819    externalEntityRefHandler() returns. It is up to the implementation of
820    the externalEntityRefHandler() to call XML_StopParser() on the parent
821    parser (recursively), if one wants to stop parsing altogether.
822 
823    When suspended, parsing can be resumed by calling XML_ResumeParser().
824 */
825 XMLPARSEAPI(enum XML_Status)
826 XML_StopParser(XML_Parser parser, XML_Bool resumable);
827 
828 /* Resumes parsing after it has been suspended with XML_StopParser().
829    Must not be called from within a handler call-back. Returns same
830    status codes as XML_Parse() or XML_ParseBuffer().
831    Additional error code XML_ERROR_NOT_SUSPENDED possible.
832 
833    *Note*:
834    This must be called on the most deeply nested child parser instance
835    first, and on its parent parser only after the child parser has finished,
836    to be applied recursively until the document entity's parser is restarted.
837    That is, the parent parser will not resume by itself and it is up to the
838    application to call XML_ResumeParser() on it at the appropriate moment.
839 */
840 XMLPARSEAPI(enum XML_Status)
841 XML_ResumeParser(XML_Parser parser);
842 
843 enum XML_Parsing { XML_INITIALIZED, XML_PARSING, XML_FINISHED, XML_SUSPENDED };
844 
845 typedef struct {
846   enum XML_Parsing parsing;
847   XML_Bool finalBuffer;
848 } XML_ParsingStatus;
849 
850 /* Returns status of parser with respect to being initialized, parsing,
851    finished, or suspended and processing the final buffer.
852    XXX XML_Parse() and XML_ParseBuffer() should return XML_ParsingStatus,
853    XXX with XML_FINISHED_OK or XML_FINISHED_ERROR replacing XML_FINISHED
854 */
855 XMLPARSEAPI(void)
856 XML_GetParsingStatus(XML_Parser parser, XML_ParsingStatus *status);
857 
858 /* Creates an XML_Parser object that can parse an external general
859    entity; context is a '\0'-terminated string specifying the parse
860    context; encoding is a '\0'-terminated string giving the name of
861    the externally specified encoding, or NULL if there is no
862    externally specified encoding.  The context string consists of a
863    sequence of tokens separated by formfeeds (\f); a token consisting
864    of a name specifies that the general entity of the name is open; a
865    token of the form prefix=uri specifies the namespace for a
866    particular prefix; a token of the form =uri specifies the default
867    namespace.  This can be called at any point after the first call to
868    an ExternalEntityRefHandler so longer as the parser has not yet
869    been freed.  The new parser is completely independent and may
870    safely be used in a separate thread.  The handlers and userData are
871    initialized from the parser argument.  Returns NULL if out of memory.
872    Otherwise returns a new XML_Parser object.
873 */
874 XMLPARSEAPI(XML_Parser)
875 XML_ExternalEntityParserCreate(XML_Parser parser, const XML_Char *context,
876                                const XML_Char *encoding);
877 
878 enum XML_ParamEntityParsing {
879   XML_PARAM_ENTITY_PARSING_NEVER,
880   XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE,
881   XML_PARAM_ENTITY_PARSING_ALWAYS
882 };
883 
884 /* Controls parsing of parameter entities (including the external DTD
885    subset). If parsing of parameter entities is enabled, then
886    references to external parameter entities (including the external
887    DTD subset) will be passed to the handler set with
888    XML_SetExternalEntityRefHandler.  The context passed will be 0.
889 
890    Unlike external general entities, external parameter entities can
891    only be parsed synchronously.  If the external parameter entity is
892    to be parsed, it must be parsed during the call to the external
893    entity ref handler: the complete sequence of
894    XML_ExternalEntityParserCreate, XML_Parse/XML_ParseBuffer and
895    XML_ParserFree calls must be made during this call.  After
896    XML_ExternalEntityParserCreate has been called to create the parser
897    for the external parameter entity (context must be 0 for this
898    call), it is illegal to make any calls on the old parser until
899    XML_ParserFree has been called on the newly created parser.
900    If the library has been compiled without support for parameter
901    entity parsing (ie without XML_DTD being defined), then
902    XML_SetParamEntityParsing will return 0 if parsing of parameter
903    entities is requested; otherwise it will return non-zero.
904    Note: If XML_SetParamEntityParsing is called after XML_Parse or
905       XML_ParseBuffer, then it has no effect and will always return 0.
906    Note: If parser == NULL, the function will do nothing and return 0.
907 */
908 XMLPARSEAPI(int)
909 XML_SetParamEntityParsing(XML_Parser parser,
910                           enum XML_ParamEntityParsing parsing);
911 
912 /* Sets the hash salt to use for internal hash calculations.
913    Helps in preventing DoS attacks based on predicting hash
914    function behavior. This must be called before parsing is started.
915    Returns 1 if successful, 0 when called after parsing has started.
916    Note: If parser == NULL, the function will do nothing and return 0.
917 */
918 XMLPARSEAPI(int)
919 XML_SetHashSalt(XML_Parser parser, unsigned long hash_salt);
920 
921 /* If XML_Parse or XML_ParseBuffer have returned XML_STATUS_ERROR, then
922    XML_GetErrorCode returns information about the error.
923 */
924 XMLPARSEAPI(enum XML_Error)
925 XML_GetErrorCode(XML_Parser parser);
926 
927 /* These functions return information about the current parse
928    location.  They may be called from any callback called to report
929    some parse event; in this case the location is the location of the
930    first of the sequence of characters that generated the event.  When
931    called from callbacks generated by declarations in the document
932    prologue, the location identified isn't as neatly defined, but will
933    be within the relevant markup.  When called outside of the callback
934    functions, the position indicated will be just past the last parse
935    event (regardless of whether there was an associated callback).
936 
937    They may also be called after returning from a call to XML_Parse
938    or XML_ParseBuffer.  If the return value is XML_STATUS_ERROR then
939    the location is the location of the character at which the error
940    was detected; otherwise the location is the location of the last
941    parse event, as described above.
942 
943    Note: XML_GetCurrentLineNumber and XML_GetCurrentColumnNumber
944    return 0 to indicate an error.
945    Note: XML_GetCurrentByteIndex returns -1 to indicate an error.
946 */
947 XMLPARSEAPI(XML_Size) XML_GetCurrentLineNumber(XML_Parser parser);
948 XMLPARSEAPI(XML_Size) XML_GetCurrentColumnNumber(XML_Parser parser);
949 XMLPARSEAPI(XML_Index) XML_GetCurrentByteIndex(XML_Parser parser);
950 
951 /* Return the number of bytes in the current event.
952    Returns 0 if the event is in an internal entity.
953 */
954 XMLPARSEAPI(int)
955 XML_GetCurrentByteCount(XML_Parser parser);
956 
957 /* If XML_CONTEXT_BYTES is >=1, returns the input buffer, sets
958    the integer pointed to by offset to the offset within this buffer
959    of the current parse position, and sets the integer pointed to by size
960    to the size of this buffer (the number of input bytes). Otherwise
961    returns a NULL pointer. Also returns a NULL pointer if a parse isn't
962    active.
963 
964    NOTE: The character pointer returned should not be used outside
965    the handler that makes the call.
966 */
967 XMLPARSEAPI(const char *)
968 XML_GetInputContext(XML_Parser parser, int *offset, int *size);
969 
970 /* For backwards compatibility with previous versions. */
971 #define XML_GetErrorLineNumber XML_GetCurrentLineNumber
972 #define XML_GetErrorColumnNumber XML_GetCurrentColumnNumber
973 #define XML_GetErrorByteIndex XML_GetCurrentByteIndex
974 
975 /* Frees the content model passed to the element declaration handler */
976 XMLPARSEAPI(void)
977 XML_FreeContentModel(XML_Parser parser, XML_Content *model);
978 
979 /* Exposing the memory handling functions used in Expat */
980 XMLPARSEAPI(void *)
981 XML_ATTR_MALLOC
982 XML_ATTR_ALLOC_SIZE(2)
983 XML_MemMalloc(XML_Parser parser, size_t size);
984 
985 XMLPARSEAPI(void *)
986 XML_ATTR_ALLOC_SIZE(3)
987 XML_MemRealloc(XML_Parser parser, void *ptr, size_t size);
988 
989 XMLPARSEAPI(void)
990 XML_MemFree(XML_Parser parser, void *ptr);
991 
992 /* Frees memory used by the parser. */
993 XMLPARSEAPI(void)
994 XML_ParserFree(XML_Parser parser);
995 
996 /* Returns a string describing the error. */
997 XMLPARSEAPI(const XML_LChar *)
998 XML_ErrorString(enum XML_Error code);
999 
1000 /* Return a string containing the version number of this expat */
1001 XMLPARSEAPI(const XML_LChar *)
1002 XML_ExpatVersion(void);
1003 
1004 typedef struct {
1005   int major;
1006   int minor;
1007   int micro;
1008 } XML_Expat_Version;
1009 
1010 /* Return an XML_Expat_Version structure containing numeric version
1011    number information for this version of expat.
1012 */
1013 XMLPARSEAPI(XML_Expat_Version)
1014 XML_ExpatVersionInfo(void);
1015 
1016 /* Added in Expat 1.95.5. */
1017 enum XML_FeatureEnum {
1018   XML_FEATURE_END = 0,
1019   XML_FEATURE_UNICODE,
1020   XML_FEATURE_UNICODE_WCHAR_T,
1021   XML_FEATURE_DTD,
1022   XML_FEATURE_CONTEXT_BYTES,
1023   XML_FEATURE_MIN_SIZE,
1024   XML_FEATURE_SIZEOF_XML_CHAR,
1025   XML_FEATURE_SIZEOF_XML_LCHAR,
1026   XML_FEATURE_NS,
1027   XML_FEATURE_LARGE_SIZE,
1028   XML_FEATURE_ATTR_INFO,
1029   /* Added in Expat 2.4.0. */
1030   XML_FEATURE_BILLION_LAUGHS_ATTACK_PROTECTION_MAXIMUM_AMPLIFICATION_DEFAULT,
1031   XML_FEATURE_BILLION_LAUGHS_ATTACK_PROTECTION_ACTIVATION_THRESHOLD_DEFAULT,
1032   /* Added in Expat 2.6.0. */
1033   XML_FEATURE_GE
1034   /* Additional features must be added to the end of this enum. */
1035 };
1036 
1037 typedef struct {
1038   enum XML_FeatureEnum feature;
1039   const XML_LChar *name;
1040   long int value;
1041 } XML_Feature;
1042 
1043 XMLPARSEAPI(const XML_Feature *)
1044 XML_GetFeatureList(void);
1045 
1046 #if defined(XML_DTD) || (defined(XML_GE) && XML_GE == 1)
1047 /* Added in Expat 2.4.0 for XML_DTD defined and
1048  * added in Expat 2.6.0 for XML_GE == 1. */
1049 XMLPARSEAPI(XML_Bool)
1050 XML_SetBillionLaughsAttackProtectionMaximumAmplification(
1051     XML_Parser parser, float maximumAmplificationFactor);
1052 
1053 /* Added in Expat 2.4.0 for XML_DTD defined and
1054  * added in Expat 2.6.0 for XML_GE == 1. */
1055 XMLPARSEAPI(XML_Bool)
1056 XML_SetBillionLaughsAttackProtectionActivationThreshold(
1057     XML_Parser parser, unsigned long long activationThresholdBytes);
1058 #endif
1059 
1060 /* Added in Expat 2.6.0. */
1061 XMLPARSEAPI(XML_Bool)
1062 XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled);
1063 
1064 /* Expat follows the semantic versioning convention.
1065    See https://semver.org
1066 */
1067 #define XML_MAJOR_VERSION 2
1068 #define XML_MINOR_VERSION 6
1069 #define XML_MICRO_VERSION 3
1070 
1071 #ifdef __cplusplus
1072 }
1073 #endif
1074 
1075 #endif /* not Expat_INCLUDED */
1076