2
0

xmltok.h 9.7 KB

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