2
0

xmltok.h 11 KB

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