xmltok.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2000 Clark Cooper <coopercc@users.sourceforge.net>
  10. Copyright (c) 2002 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
  11. Copyright (c) 2002-2005 Karl Waclawek <karl@waclawek.net>
  12. Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org>
  13. Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
  14. Licensed under the MIT license:
  15. Permission is hereby granted, free of charge, to any person obtaining
  16. a copy of this software and associated documentation files (the
  17. "Software"), to deal in the Software without restriction, including
  18. without limitation the rights to use, copy, modify, merge, publish,
  19. distribute, sublicense, and/or sell copies of the Software, and to permit
  20. persons to whom the Software is furnished to do so, subject to the
  21. following conditions:
  22. The above copyright notice and this permission notice shall be included
  23. in all copies or substantial portions of the Software.
  24. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  27. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  28. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  29. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  30. USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. */
  32. #ifndef XmlTok_INCLUDED
  33. #define XmlTok_INCLUDED 1
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /* The following token may be returned by XmlContentTok */
  38. #define XML_TOK_TRAILING_RSQB \
  39. -5 /* ] or ]] at the end of the scan; might be \
  40. start of illegal ]]> sequence */
  41. /* The following tokens may be returned by both XmlPrologTok and
  42. XmlContentTok.
  43. */
  44. #define XML_TOK_NONE -4 /* The string to be scanned is empty */
  45. #define XML_TOK_TRAILING_CR \
  46. -3 /* A CR at the end of the scan; \
  47. might be part of CRLF sequence */
  48. #define XML_TOK_PARTIAL_CHAR -2 /* only part of a multibyte sequence */
  49. #define XML_TOK_PARTIAL -1 /* only part of a token */
  50. #define XML_TOK_INVALID 0
  51. /* The following tokens are returned by XmlContentTok; some are also
  52. returned by XmlAttributeValueTok, XmlEntityTok, XmlCdataSectionTok.
  53. */
  54. #define XML_TOK_START_TAG_WITH_ATTS 1
  55. #define XML_TOK_START_TAG_NO_ATTS 2
  56. #define XML_TOK_EMPTY_ELEMENT_WITH_ATTS 3 /* empty element tag <e/> */
  57. #define XML_TOK_EMPTY_ELEMENT_NO_ATTS 4
  58. #define XML_TOK_END_TAG 5
  59. #define XML_TOK_DATA_CHARS 6
  60. #define XML_TOK_DATA_NEWLINE 7
  61. #define XML_TOK_CDATA_SECT_OPEN 8
  62. #define XML_TOK_ENTITY_REF 9
  63. #define XML_TOK_CHAR_REF 10 /* numeric character reference */
  64. /* The following tokens may be returned by both XmlPrologTok and
  65. XmlContentTok.
  66. */
  67. #define XML_TOK_PI 11 /* processing instruction */
  68. #define XML_TOK_XML_DECL 12 /* XML decl or text decl */
  69. #define XML_TOK_COMMENT 13
  70. #define XML_TOK_BOM 14 /* Byte order mark */
  71. /* The following tokens are returned only by XmlPrologTok */
  72. #define XML_TOK_PROLOG_S 15
  73. #define XML_TOK_DECL_OPEN 16 /* <!foo */
  74. #define XML_TOK_DECL_CLOSE 17 /* > */
  75. #define XML_TOK_NAME 18
  76. #define XML_TOK_NMTOKEN 19
  77. #define XML_TOK_POUND_NAME 20 /* #name */
  78. #define XML_TOK_OR 21 /* | */
  79. #define XML_TOK_PERCENT 22
  80. #define XML_TOK_OPEN_PAREN 23
  81. #define XML_TOK_CLOSE_PAREN 24
  82. #define XML_TOK_OPEN_BRACKET 25
  83. #define XML_TOK_CLOSE_BRACKET 26
  84. #define XML_TOK_LITERAL 27
  85. #define XML_TOK_PARAM_ENTITY_REF 28
  86. #define XML_TOK_INSTANCE_START 29
  87. /* The following occur only in element type declarations */
  88. #define XML_TOK_NAME_QUESTION 30 /* name? */
  89. #define XML_TOK_NAME_ASTERISK 31 /* name* */
  90. #define XML_TOK_NAME_PLUS 32 /* name+ */
  91. #define XML_TOK_COND_SECT_OPEN 33 /* <![ */
  92. #define XML_TOK_COND_SECT_CLOSE 34 /* ]]> */
  93. #define XML_TOK_CLOSE_PAREN_QUESTION 35 /* )? */
  94. #define XML_TOK_CLOSE_PAREN_ASTERISK 36 /* )* */
  95. #define XML_TOK_CLOSE_PAREN_PLUS 37 /* )+ */
  96. #define XML_TOK_COMMA 38
  97. /* The following token is returned only by XmlAttributeValueTok */
  98. #define XML_TOK_ATTRIBUTE_VALUE_S 39
  99. /* The following token is returned only by XmlCdataSectionTok */
  100. #define XML_TOK_CDATA_SECT_CLOSE 40
  101. /* With namespace processing this is returned by XmlPrologTok for a
  102. name with a colon.
  103. */
  104. #define XML_TOK_PREFIXED_NAME 41
  105. #ifdef XML_DTD
  106. # define XML_TOK_IGNORE_SECT 42
  107. #endif /* XML_DTD */
  108. #ifdef XML_DTD
  109. # define XML_N_STATES 4
  110. #else /* not XML_DTD */
  111. # define XML_N_STATES 3
  112. #endif /* not XML_DTD */
  113. #define XML_PROLOG_STATE 0
  114. #define XML_CONTENT_STATE 1
  115. #define XML_CDATA_SECTION_STATE 2
  116. #ifdef XML_DTD
  117. # define XML_IGNORE_SECTION_STATE 3
  118. #endif /* XML_DTD */
  119. #define XML_N_LITERAL_TYPES 2
  120. #define XML_ATTRIBUTE_VALUE_LITERAL 0
  121. #define XML_ENTITY_VALUE_LITERAL 1
  122. /* The size of the buffer passed to XmlUtf8Encode must be at least this. */
  123. #define XML_UTF8_ENCODE_MAX 4
  124. /* The size of the buffer passed to XmlUtf16Encode must be at least this. */
  125. #define XML_UTF16_ENCODE_MAX 2
  126. typedef struct position {
  127. /* first line and first column are 0 not 1 */
  128. XML_Size lineNumber;
  129. XML_Size columnNumber;
  130. } POSITION;
  131. typedef struct {
  132. const char *name;
  133. const char *valuePtr;
  134. const char *valueEnd;
  135. char normalized;
  136. } ATTRIBUTE;
  137. struct encoding;
  138. typedef struct encoding ENCODING;
  139. typedef int(PTRCALL *SCANNER)(const ENCODING *, const char *, const char *,
  140. const char **);
  141. enum XML_Convert_Result {
  142. XML_CONVERT_COMPLETED = 0,
  143. XML_CONVERT_INPUT_INCOMPLETE = 1,
  144. XML_CONVERT_OUTPUT_EXHAUSTED
  145. = 2 /* and therefore potentially input remaining as well */
  146. };
  147. struct encoding {
  148. SCANNER scanners[XML_N_STATES];
  149. SCANNER literalScanners[XML_N_LITERAL_TYPES];
  150. int(PTRCALL *nameMatchesAscii)(const ENCODING *, const char *, const char *,
  151. const char *);
  152. int(PTRFASTCALL *nameLength)(const ENCODING *, const char *);
  153. const char *(PTRFASTCALL *skipS)(const ENCODING *, const char *);
  154. int(PTRCALL *getAtts)(const ENCODING *enc, const char *ptr, int attsMax,
  155. ATTRIBUTE *atts);
  156. int(PTRFASTCALL *charRefNumber)(const ENCODING *enc, const char *ptr);
  157. int(PTRCALL *predefinedEntityName)(const ENCODING *, const char *,
  158. const char *);
  159. void(PTRCALL *updatePosition)(const ENCODING *, const char *ptr,
  160. const char *end, POSITION *);
  161. int(PTRCALL *isPublicId)(const ENCODING *enc, const char *ptr,
  162. const char *end, const char **badPtr);
  163. enum XML_Convert_Result(PTRCALL *utf8Convert)(const ENCODING *enc,
  164. const char **fromP,
  165. const char *fromLim, char **toP,
  166. const char *toLim);
  167. enum XML_Convert_Result(PTRCALL *utf16Convert)(const ENCODING *enc,
  168. const char **fromP,
  169. const char *fromLim,
  170. unsigned short **toP,
  171. const unsigned short *toLim);
  172. int minBytesPerChar;
  173. char isUtf8;
  174. char isUtf16;
  175. };
  176. /* Scan the string starting at ptr until the end of the next complete
  177. token, but do not scan past eptr. Return an integer giving the
  178. type of token.
  179. Return XML_TOK_NONE when ptr == eptr; nextTokPtr will not be set.
  180. Return XML_TOK_PARTIAL when the string does not contain a complete
  181. token; nextTokPtr will not be set.
  182. Return XML_TOK_INVALID when the string does not start a valid
  183. token; nextTokPtr will be set to point to the character which made
  184. the token invalid.
  185. Otherwise the string starts with a valid token; nextTokPtr will be
  186. set to point to the character following the end of that token.
  187. Each data character counts as a single token, but adjacent data
  188. characters may be returned together. Similarly for characters in
  189. the prolog outside literals, comments and processing instructions.
  190. */
  191. #define XmlTok(enc, state, ptr, end, nextTokPtr) \
  192. (((enc)->scanners[state])(enc, ptr, end, nextTokPtr))
  193. #define XmlPrologTok(enc, ptr, end, nextTokPtr) \
  194. XmlTok(enc, XML_PROLOG_STATE, ptr, end, nextTokPtr)
  195. #define XmlContentTok(enc, ptr, end, nextTokPtr) \
  196. XmlTok(enc, XML_CONTENT_STATE, ptr, end, nextTokPtr)
  197. #define XmlCdataSectionTok(enc, ptr, end, nextTokPtr) \
  198. XmlTok(enc, XML_CDATA_SECTION_STATE, ptr, end, nextTokPtr)
  199. #ifdef XML_DTD
  200. # define XmlIgnoreSectionTok(enc, ptr, end, nextTokPtr) \
  201. XmlTok(enc, XML_IGNORE_SECTION_STATE, ptr, end, nextTokPtr)
  202. #endif /* XML_DTD */
  203. /* This is used for performing a 2nd-level tokenization on the content
  204. of a literal that has already been returned by XmlTok.
  205. */
  206. #define XmlLiteralTok(enc, literalType, ptr, end, nextTokPtr) \
  207. (((enc)->literalScanners[literalType])(enc, ptr, end, nextTokPtr))
  208. #define XmlAttributeValueTok(enc, ptr, end, nextTokPtr) \
  209. XmlLiteralTok(enc, XML_ATTRIBUTE_VALUE_LITERAL, ptr, end, nextTokPtr)
  210. #define XmlEntityValueTok(enc, ptr, end, nextTokPtr) \
  211. XmlLiteralTok(enc, XML_ENTITY_VALUE_LITERAL, ptr, end, nextTokPtr)
  212. #define XmlNameMatchesAscii(enc, ptr1, end1, ptr2) \
  213. (((enc)->nameMatchesAscii)(enc, ptr1, end1, ptr2))
  214. #define XmlNameLength(enc, ptr) (((enc)->nameLength)(enc, ptr))
  215. #define XmlSkipS(enc, ptr) (((enc)->skipS)(enc, ptr))
  216. #define XmlGetAttributes(enc, ptr, attsMax, atts) \
  217. (((enc)->getAtts)(enc, ptr, attsMax, atts))
  218. #define XmlCharRefNumber(enc, ptr) (((enc)->charRefNumber)(enc, ptr))
  219. #define XmlPredefinedEntityName(enc, ptr, end) \
  220. (((enc)->predefinedEntityName)(enc, ptr, end))
  221. #define XmlUpdatePosition(enc, ptr, end, pos) \
  222. (((enc)->updatePosition)(enc, ptr, end, pos))
  223. #define XmlIsPublicId(enc, ptr, end, badPtr) \
  224. (((enc)->isPublicId)(enc, ptr, end, badPtr))
  225. #define XmlUtf8Convert(enc, fromP, fromLim, toP, toLim) \
  226. (((enc)->utf8Convert)(enc, fromP, fromLim, toP, toLim))
  227. #define XmlUtf16Convert(enc, fromP, fromLim, toP, toLim) \
  228. (((enc)->utf16Convert)(enc, fromP, fromLim, toP, toLim))
  229. typedef struct {
  230. ENCODING initEnc;
  231. const ENCODING **encPtr;
  232. } INIT_ENCODING;
  233. int XmlParseXmlDecl(int isGeneralTextEntity, const ENCODING *enc,
  234. const char *ptr, const char *end, const char **badPtr,
  235. const char **versionPtr, const char **versionEndPtr,
  236. const char **encodingNamePtr,
  237. const ENCODING **namedEncodingPtr, int *standalonePtr);
  238. int XmlInitEncoding(INIT_ENCODING *p, const ENCODING **encPtr,
  239. const char *name);
  240. const ENCODING *XmlGetUtf8InternalEncoding(void);
  241. const ENCODING *XmlGetUtf16InternalEncoding(void);
  242. int FASTCALL XmlUtf8Encode(int charNumber, char *buf);
  243. int FASTCALL XmlUtf16Encode(int charNumber, unsigned short *buf);
  244. int XmlSizeOfUnknownEncoding(void);
  245. typedef int(XMLCALL *CONVERTER)(void *userData, const char *p);
  246. ENCODING *XmlInitUnknownEncoding(void *mem, int *table, CONVERTER convert,
  247. void *userData);
  248. int XmlParseXmlDeclNS(int isGeneralTextEntity, const ENCODING *enc,
  249. const char *ptr, const char *end, const char **badPtr,
  250. const char **versionPtr, const char **versionEndPtr,
  251. const char **encodingNamePtr,
  252. const ENCODING **namedEncodingPtr, int *standalonePtr);
  253. int XmlInitEncodingNS(INIT_ENCODING *p, const ENCODING **encPtr,
  254. const char *name);
  255. const ENCODING *XmlGetUtf8InternalEncodingNS(void);
  256. const ENCODING *XmlGetUtf16InternalEncodingNS(void);
  257. ENCODING *XmlInitUnknownEncodingNS(void *mem, int *table, CONVERTER convert,
  258. void *userData);
  259. #ifdef __cplusplus
  260. }
  261. #endif
  262. #endif /* not XmlTok_INCLUDED */