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