xmltok.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
  2. See the file COPYING for copying permission.
  3. */
  4. #ifndef XmlTok_INCLUDED
  5. #define XmlTok_INCLUDED 1
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* The following token may be returned by XmlContentTok */
  10. #define XML_TOK_TRAILING_RSQB -5 /* ] or ]] at the end of the scan; might be
  11. start of illegal ]]> sequence */
  12. /* The following tokens may be returned by both XmlPrologTok and
  13. XmlContentTok.
  14. */
  15. #define XML_TOK_NONE -4 /* The string to be scanned is empty */
  16. #define XML_TOK_TRAILING_CR -3 /* A CR at the end of the scan;
  17. might be part of CRLF sequence */
  18. #define XML_TOK_PARTIAL_CHAR -2 /* only part of a multibyte sequence */
  19. #define XML_TOK_PARTIAL -1 /* only part of a token */
  20. #define XML_TOK_INVALID 0
  21. /* The following tokens are returned by XmlContentTok; some are also
  22. returned by XmlAttributeValueTok, XmlEntityTok, XmlCdataSectionTok.
  23. */
  24. #define XML_TOK_START_TAG_WITH_ATTS 1
  25. #define XML_TOK_START_TAG_NO_ATTS 2
  26. #define XML_TOK_EMPTY_ELEMENT_WITH_ATTS 3 /* empty element tag <e/> */
  27. #define XML_TOK_EMPTY_ELEMENT_NO_ATTS 4
  28. #define XML_TOK_END_TAG 5
  29. #define XML_TOK_DATA_CHARS 6
  30. #define XML_TOK_DATA_NEWLINE 7
  31. #define XML_TOK_CDATA_SECT_OPEN 8
  32. #define XML_TOK_ENTITY_REF 9
  33. #define XML_TOK_CHAR_REF 10 /* numeric character reference */
  34. /* The following tokens may be returned by both XmlPrologTok and
  35. XmlContentTok.
  36. */
  37. #define XML_TOK_PI 11 /* processing instruction */
  38. #define XML_TOK_XML_DECL 12 /* XML decl or text decl */
  39. #define XML_TOK_COMMENT 13
  40. #define XML_TOK_BOM 14 /* Byte order mark */
  41. /* The following tokens are returned only by XmlPrologTok */
  42. #define XML_TOK_PROLOG_S 15
  43. #define XML_TOK_DECL_OPEN 16 /* <!foo */
  44. #define XML_TOK_DECL_CLOSE 17 /* > */
  45. #define XML_TOK_NAME 18
  46. #define XML_TOK_NMTOKEN 19
  47. #define XML_TOK_POUND_NAME 20 /* #name */
  48. #define XML_TOK_OR 21 /* | */
  49. #define XML_TOK_PERCENT 22
  50. #define XML_TOK_OPEN_PAREN 23
  51. #define XML_TOK_CLOSE_PAREN 24
  52. #define XML_TOK_OPEN_BRACKET 25
  53. #define XML_TOK_CLOSE_BRACKET 26
  54. #define XML_TOK_LITERAL 27
  55. #define XML_TOK_PARAM_ENTITY_REF 28
  56. #define XML_TOK_INSTANCE_START 29
  57. /* The following occur only in element type declarations */
  58. #define XML_TOK_NAME_QUESTION 30 /* name? */
  59. #define XML_TOK_NAME_ASTERISK 31 /* name* */
  60. #define XML_TOK_NAME_PLUS 32 /* name+ */
  61. #define XML_TOK_COND_SECT_OPEN 33 /* <![ */
  62. #define XML_TOK_COND_SECT_CLOSE 34 /* ]]> */
  63. #define XML_TOK_CLOSE_PAREN_QUESTION 35 /* )? */
  64. #define XML_TOK_CLOSE_PAREN_ASTERISK 36 /* )* */
  65. #define XML_TOK_CLOSE_PAREN_PLUS 37 /* )+ */
  66. #define XML_TOK_COMMA 38
  67. /* The following token is returned only by XmlAttributeValueTok */
  68. #define XML_TOK_ATTRIBUTE_VALUE_S 39
  69. /* The following token is returned only by XmlCdataSectionTok */
  70. #define XML_TOK_CDATA_SECT_CLOSE 40
  71. /* With namespace processing this is returned by XmlPrologTok for a
  72. name with a colon.
  73. */
  74. #define XML_TOK_PREFIXED_NAME 41
  75. #ifdef XML_DTD
  76. #define XML_TOK_IGNORE_SECT 42
  77. #endif /* XML_DTD */
  78. #ifdef XML_DTD
  79. #define XML_N_STATES 4
  80. #else /* not XML_DTD */
  81. #define XML_N_STATES 3
  82. #endif /* not XML_DTD */
  83. #define XML_PROLOG_STATE 0
  84. #define XML_CONTENT_STATE 1
  85. #define XML_CDATA_SECTION_STATE 2
  86. #ifdef XML_DTD
  87. #define XML_IGNORE_SECTION_STATE 3
  88. #endif /* XML_DTD */
  89. #define XML_N_LITERAL_TYPES 2
  90. #define XML_ATTRIBUTE_VALUE_LITERAL 0
  91. #define XML_ENTITY_VALUE_LITERAL 1
  92. /* The size of the buffer passed to XmlUtf8Encode must be at least this. */
  93. #define XML_UTF8_ENCODE_MAX 4
  94. /* The size of the buffer passed to XmlUtf16Encode must be at least this. */
  95. #define XML_UTF16_ENCODE_MAX 2
  96. typedef struct position {
  97. /* first line and first column are 0 not 1 */
  98. XML_Size lineNumber;
  99. XML_Size columnNumber;
  100. } POSITION;
  101. typedef struct {
  102. const char *name;
  103. const char *valuePtr;
  104. const char *valueEnd;
  105. char normalized;
  106. } ATTRIBUTE;
  107. struct encoding;
  108. typedef struct encoding ENCODING;
  109. typedef int (PTRCALL *SCANNER)(const ENCODING *,
  110. const char *,
  111. const char *,
  112. const char **);
  113. enum XML_Convert_Result {
  114. XML_CONVERT_COMPLETED = 0,
  115. XML_CONVERT_INPUT_INCOMPLETE = 1,
  116. XML_CONVERT_OUTPUT_EXHAUSTED = 2 /* and therefore potentially input remaining as well */
  117. };
  118. struct encoding {
  119. SCANNER scanners[XML_N_STATES];
  120. SCANNER literalScanners[XML_N_LITERAL_TYPES];
  121. int (PTRCALL *sameName)(const ENCODING *,
  122. const char *,
  123. const char *);
  124. int (PTRCALL *nameMatchesAscii)(const ENCODING *,
  125. const char *,
  126. const char *,
  127. const char *);
  128. int (PTRFASTCALL *nameLength)(const ENCODING *, const char *);
  129. const char *(PTRFASTCALL *skipS)(const ENCODING *, const char *);
  130. int (PTRCALL *getAtts)(const ENCODING *enc,
  131. const char *ptr,
  132. int attsMax,
  133. ATTRIBUTE *atts);
  134. int (PTRFASTCALL *charRefNumber)(const ENCODING *enc, const char *ptr);
  135. int (PTRCALL *predefinedEntityName)(const ENCODING *,
  136. const char *,
  137. const char *);
  138. void (PTRCALL *updatePosition)(const ENCODING *,
  139. const char *ptr,
  140. const char *end,
  141. POSITION *);
  142. int (PTRCALL *isPublicId)(const ENCODING *enc,
  143. const char *ptr,
  144. const char *end,
  145. const char **badPtr);
  146. enum XML_Convert_Result (PTRCALL *utf8Convert)(const ENCODING *enc,
  147. const char **fromP,
  148. const char *fromLim,
  149. char **toP,
  150. const char *toLim);
  151. enum XML_Convert_Result (PTRCALL *utf16Convert)(const ENCODING *enc,
  152. const char **fromP,
  153. const char *fromLim,
  154. unsigned short **toP,
  155. const unsigned short *toLim);
  156. int minBytesPerChar;
  157. char isUtf8;
  158. char isUtf16;
  159. };
  160. /* Scan the string starting at ptr until the end of the next complete
  161. token, but do not scan past eptr. Return an integer giving the
  162. type of token.
  163. Return XML_TOK_NONE when ptr == eptr; nextTokPtr will not be set.
  164. Return XML_TOK_PARTIAL when the string does not contain a complete
  165. token; nextTokPtr will not be set.
  166. Return XML_TOK_INVALID when the string does not start a valid
  167. token; nextTokPtr will be set to point to the character which made
  168. the token invalid.
  169. Otherwise the string starts with a valid token; nextTokPtr will be
  170. set to point to the character following the end of that token.
  171. Each data character counts as a single token, but adjacent data
  172. characters may be returned together. Similarly for characters in
  173. the prolog outside literals, comments and processing instructions.
  174. */
  175. #define XmlTok(enc, state, ptr, end, nextTokPtr) \
  176. (((enc)->scanners[state])(enc, ptr, end, nextTokPtr))
  177. #define XmlPrologTok(enc, ptr, end, nextTokPtr) \
  178. XmlTok(enc, XML_PROLOG_STATE, ptr, end, nextTokPtr)
  179. #define XmlContentTok(enc, ptr, end, nextTokPtr) \
  180. XmlTok(enc, XML_CONTENT_STATE, ptr, end, nextTokPtr)
  181. #define XmlCdataSectionTok(enc, ptr, end, nextTokPtr) \
  182. XmlTok(enc, XML_CDATA_SECTION_STATE, ptr, end, nextTokPtr)
  183. #ifdef XML_DTD
  184. #define XmlIgnoreSectionTok(enc, ptr, end, nextTokPtr) \
  185. XmlTok(enc, XML_IGNORE_SECTION_STATE, ptr, end, nextTokPtr)
  186. #endif /* XML_DTD */
  187. /* This is used for performing a 2nd-level tokenization on the content
  188. of a literal that has already been returned by XmlTok.
  189. */
  190. #define XmlLiteralTok(enc, literalType, ptr, end, nextTokPtr) \
  191. (((enc)->literalScanners[literalType])(enc, ptr, end, nextTokPtr))
  192. #define XmlAttributeValueTok(enc, ptr, end, nextTokPtr) \
  193. XmlLiteralTok(enc, XML_ATTRIBUTE_VALUE_LITERAL, ptr, end, nextTokPtr)
  194. #define XmlEntityValueTok(enc, ptr, end, nextTokPtr) \
  195. XmlLiteralTok(enc, XML_ENTITY_VALUE_LITERAL, ptr, end, nextTokPtr)
  196. #define XmlSameName(enc, ptr1, ptr2) (((enc)->sameName)(enc, ptr1, ptr2))
  197. #define XmlNameMatchesAscii(enc, ptr1, end1, ptr2) \
  198. (((enc)->nameMatchesAscii)(enc, ptr1, end1, ptr2))
  199. #define XmlNameLength(enc, ptr) \
  200. (((enc)->nameLength)(enc, ptr))
  201. #define XmlSkipS(enc, ptr) \
  202. (((enc)->skipS)(enc, ptr))
  203. #define XmlGetAttributes(enc, ptr, attsMax, atts) \
  204. (((enc)->getAtts)(enc, ptr, attsMax, atts))
  205. #define XmlCharRefNumber(enc, ptr) \
  206. (((enc)->charRefNumber)(enc, ptr))
  207. #define XmlPredefinedEntityName(enc, ptr, end) \
  208. (((enc)->predefinedEntityName)(enc, ptr, end))
  209. #define XmlUpdatePosition(enc, ptr, end, pos) \
  210. (((enc)->updatePosition)(enc, ptr, end, pos))
  211. #define XmlIsPublicId(enc, ptr, end, badPtr) \
  212. (((enc)->isPublicId)(enc, ptr, end, badPtr))
  213. #define XmlUtf8Convert(enc, fromP, fromLim, toP, toLim) \
  214. (((enc)->utf8Convert)(enc, fromP, fromLim, toP, toLim))
  215. #define XmlUtf16Convert(enc, fromP, fromLim, toP, toLim) \
  216. (((enc)->utf16Convert)(enc, fromP, fromLim, toP, toLim))
  217. typedef struct {
  218. ENCODING initEnc;
  219. const ENCODING **encPtr;
  220. } INIT_ENCODING;
  221. int XmlParseXmlDecl(int isGeneralTextEntity,
  222. const ENCODING *enc,
  223. const char *ptr,
  224. const char *end,
  225. const char **badPtr,
  226. const char **versionPtr,
  227. const char **versionEndPtr,
  228. const char **encodingNamePtr,
  229. const ENCODING **namedEncodingPtr,
  230. int *standalonePtr);
  231. int XmlInitEncoding(INIT_ENCODING *, const ENCODING **, const char *name);
  232. const ENCODING *XmlGetUtf8InternalEncoding(void);
  233. const ENCODING *XmlGetUtf16InternalEncoding(void);
  234. int FASTCALL XmlUtf8Encode(int charNumber, char *buf);
  235. int FASTCALL XmlUtf16Encode(int charNumber, unsigned short *buf);
  236. int XmlSizeOfUnknownEncoding(void);
  237. typedef int (XMLCALL *CONVERTER) (void *userData, const char *p);
  238. ENCODING *
  239. XmlInitUnknownEncoding(void *mem,
  240. int *table,
  241. CONVERTER convert,
  242. void *userData);
  243. int XmlParseXmlDeclNS(int isGeneralTextEntity,
  244. const ENCODING *enc,
  245. const char *ptr,
  246. const char *end,
  247. const char **badPtr,
  248. const char **versionPtr,
  249. const char **versionEndPtr,
  250. const char **encodingNamePtr,
  251. const ENCODING **namedEncodingPtr,
  252. int *standalonePtr);
  253. int XmlInitEncodingNS(INIT_ENCODING *, const ENCODING **, const char *name);
  254. const ENCODING *XmlGetUtf8InternalEncodingNS(void);
  255. const ENCODING *XmlGetUtf16InternalEncodingNS(void);
  256. ENCODING *
  257. XmlInitUnknownEncodingNS(void *mem,
  258. int *table,
  259. CONVERTER convert,
  260. void *userData);
  261. #ifdef __cplusplus
  262. }
  263. #endif
  264. #endif /* not XmlTok_INCLUDED */