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