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