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