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