expat.h 44 KB

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