parser/expat/lib/xmltok.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
michael@0 2 See the file COPYING for copying permission.
michael@0 3 */
michael@0 4
michael@0 5 #include <stddef.h>
michael@0 6
michael@0 7 #ifdef COMPILED_FROM_DSP
michael@0 8 #include "winconfig.h"
michael@0 9 #elif defined(MACOS_CLASSIC)
michael@0 10 #include "macconfig.h"
michael@0 11 #elif defined(__amigaos4__)
michael@0 12 #include "amigaconfig.h"
michael@0 13 #else
michael@0 14 #ifdef HAVE_EXPAT_CONFIG_H
michael@0 15 #include <expat_config.h>
michael@0 16 #endif
michael@0 17 #endif /* ndef COMPILED_FROM_DSP */
michael@0 18
michael@0 19 #include "expat_external.h"
michael@0 20 #include "internal.h"
michael@0 21 #include "xmltok.h"
michael@0 22 #include "nametab.h"
michael@0 23
michael@0 24 #ifdef XML_DTD
michael@0 25 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
michael@0 26 #else
michael@0 27 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
michael@0 28 #endif
michael@0 29
michael@0 30 #define VTABLE1 \
michael@0 31 { PREFIX(prologTok), PREFIX(contentTok), \
michael@0 32 PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
michael@0 33 { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
michael@0 34 PREFIX(sameName), \
michael@0 35 PREFIX(nameMatchesAscii), \
michael@0 36 PREFIX(nameLength), \
michael@0 37 PREFIX(skipS), \
michael@0 38 PREFIX(getAtts), \
michael@0 39 PREFIX(charRefNumber), \
michael@0 40 PREFIX(predefinedEntityName), \
michael@0 41 PREFIX(updatePosition), \
michael@0 42 PREFIX(isPublicId)
michael@0 43
michael@0 44 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
michael@0 45
michael@0 46 #define UCS2_GET_NAMING(pages, hi, lo) \
michael@0 47 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
michael@0 48
michael@0 49 /* A 2 byte UTF-8 representation splits the characters 11 bits between
michael@0 50 the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
michael@0 51 pages, 3 bits to add to that index and 5 bits to generate the mask.
michael@0 52 */
michael@0 53 #define UTF8_GET_NAMING2(pages, byte) \
michael@0 54 (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
michael@0 55 + ((((byte)[0]) & 3) << 1) \
michael@0 56 + ((((byte)[1]) >> 5) & 1)] \
michael@0 57 & (1 << (((byte)[1]) & 0x1F)))
michael@0 58
michael@0 59 /* A 3 byte UTF-8 representation splits the characters 16 bits between
michael@0 60 the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
michael@0 61 into pages, 3 bits to add to that index and 5 bits to generate the
michael@0 62 mask.
michael@0 63 */
michael@0 64 #define UTF8_GET_NAMING3(pages, byte) \
michael@0 65 (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
michael@0 66 + ((((byte)[1]) >> 2) & 0xF)] \
michael@0 67 << 3) \
michael@0 68 + ((((byte)[1]) & 3) << 1) \
michael@0 69 + ((((byte)[2]) >> 5) & 1)] \
michael@0 70 & (1 << (((byte)[2]) & 0x1F)))
michael@0 71
michael@0 72 #define UTF8_GET_NAMING(pages, p, n) \
michael@0 73 ((n) == 2 \
michael@0 74 ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
michael@0 75 : ((n) == 3 \
michael@0 76 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
michael@0 77 : 0))
michael@0 78
michael@0 79 /* Detection of invalid UTF-8 sequences is based on Table 3.1B
michael@0 80 of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
michael@0 81 with the additional restriction of not allowing the Unicode
michael@0 82 code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
michael@0 83 Implementation details:
michael@0 84 (A & 0x80) == 0 means A < 0x80
michael@0 85 and
michael@0 86 (A & 0xC0) == 0xC0 means A > 0xBF
michael@0 87 */
michael@0 88
michael@0 89 #define UTF8_INVALID2(p) \
michael@0 90 ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
michael@0 91
michael@0 92 #define UTF8_INVALID3(p) \
michael@0 93 (((p)[2] & 0x80) == 0 \
michael@0 94 || \
michael@0 95 ((*p) == 0xEF && (p)[1] == 0xBF \
michael@0 96 ? \
michael@0 97 (p)[2] > 0xBD \
michael@0 98 : \
michael@0 99 ((p)[2] & 0xC0) == 0xC0) \
michael@0 100 || \
michael@0 101 ((*p) == 0xE0 \
michael@0 102 ? \
michael@0 103 (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
michael@0 104 : \
michael@0 105 ((p)[1] & 0x80) == 0 \
michael@0 106 || \
michael@0 107 ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
michael@0 108
michael@0 109 #define UTF8_INVALID4(p) \
michael@0 110 (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 \
michael@0 111 || \
michael@0 112 ((p)[2] & 0x80) == 0 || ((p)[2] & 0xC0) == 0xC0 \
michael@0 113 || \
michael@0 114 ((*p) == 0xF0 \
michael@0 115 ? \
michael@0 116 (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
michael@0 117 : \
michael@0 118 ((p)[1] & 0x80) == 0 \
michael@0 119 || \
michael@0 120 ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
michael@0 121
michael@0 122 static int PTRFASTCALL
michael@0 123 isNever(const ENCODING *enc, const char *p)
michael@0 124 {
michael@0 125 return 0;
michael@0 126 }
michael@0 127
michael@0 128 static int PTRFASTCALL
michael@0 129 utf8_isName2(const ENCODING *enc, const char *p)
michael@0 130 {
michael@0 131 return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
michael@0 132 }
michael@0 133
michael@0 134 static int PTRFASTCALL
michael@0 135 utf8_isName3(const ENCODING *enc, const char *p)
michael@0 136 {
michael@0 137 return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
michael@0 138 }
michael@0 139
michael@0 140 #define utf8_isName4 isNever
michael@0 141
michael@0 142 static int PTRFASTCALL
michael@0 143 utf8_isNmstrt2(const ENCODING *enc, const char *p)
michael@0 144 {
michael@0 145 return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
michael@0 146 }
michael@0 147
michael@0 148 static int PTRFASTCALL
michael@0 149 utf8_isNmstrt3(const ENCODING *enc, const char *p)
michael@0 150 {
michael@0 151 return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
michael@0 152 }
michael@0 153
michael@0 154 #define utf8_isNmstrt4 isNever
michael@0 155
michael@0 156 static int PTRFASTCALL
michael@0 157 utf8_isInvalid2(const ENCODING *enc, const char *p)
michael@0 158 {
michael@0 159 return UTF8_INVALID2((const unsigned char *)p);
michael@0 160 }
michael@0 161
michael@0 162 static int PTRFASTCALL
michael@0 163 utf8_isInvalid3(const ENCODING *enc, const char *p)
michael@0 164 {
michael@0 165 return UTF8_INVALID3((const unsigned char *)p);
michael@0 166 }
michael@0 167
michael@0 168 static int PTRFASTCALL
michael@0 169 utf8_isInvalid4(const ENCODING *enc, const char *p)
michael@0 170 {
michael@0 171 return UTF8_INVALID4((const unsigned char *)p);
michael@0 172 }
michael@0 173
michael@0 174 struct normal_encoding {
michael@0 175 ENCODING enc;
michael@0 176 unsigned char type[256];
michael@0 177 #ifdef XML_MIN_SIZE
michael@0 178 int (PTRFASTCALL *byteType)(const ENCODING *, const char *);
michael@0 179 int (PTRFASTCALL *isNameMin)(const ENCODING *, const char *);
michael@0 180 int (PTRFASTCALL *isNmstrtMin)(const ENCODING *, const char *);
michael@0 181 int (PTRFASTCALL *byteToAscii)(const ENCODING *, const char *);
michael@0 182 int (PTRCALL *charMatches)(const ENCODING *, const char *, int);
michael@0 183 #endif /* XML_MIN_SIZE */
michael@0 184 int (PTRFASTCALL *isName2)(const ENCODING *, const char *);
michael@0 185 int (PTRFASTCALL *isName3)(const ENCODING *, const char *);
michael@0 186 int (PTRFASTCALL *isName4)(const ENCODING *, const char *);
michael@0 187 int (PTRFASTCALL *isNmstrt2)(const ENCODING *, const char *);
michael@0 188 int (PTRFASTCALL *isNmstrt3)(const ENCODING *, const char *);
michael@0 189 int (PTRFASTCALL *isNmstrt4)(const ENCODING *, const char *);
michael@0 190 int (PTRFASTCALL *isInvalid2)(const ENCODING *, const char *);
michael@0 191 int (PTRFASTCALL *isInvalid3)(const ENCODING *, const char *);
michael@0 192 int (PTRFASTCALL *isInvalid4)(const ENCODING *, const char *);
michael@0 193 };
michael@0 194
michael@0 195 #define AS_NORMAL_ENCODING(enc) ((const struct normal_encoding *) (enc))
michael@0 196
michael@0 197 #ifdef XML_MIN_SIZE
michael@0 198
michael@0 199 #define STANDARD_VTABLE(E) \
michael@0 200 E ## byteType, \
michael@0 201 E ## isNameMin, \
michael@0 202 E ## isNmstrtMin, \
michael@0 203 E ## byteToAscii, \
michael@0 204 E ## charMatches,
michael@0 205
michael@0 206 #else
michael@0 207
michael@0 208 #define STANDARD_VTABLE(E) /* as nothing */
michael@0 209
michael@0 210 #endif
michael@0 211
michael@0 212 #define NORMAL_VTABLE(E) \
michael@0 213 E ## isName2, \
michael@0 214 E ## isName3, \
michael@0 215 E ## isName4, \
michael@0 216 E ## isNmstrt2, \
michael@0 217 E ## isNmstrt3, \
michael@0 218 E ## isNmstrt4, \
michael@0 219 E ## isInvalid2, \
michael@0 220 E ## isInvalid3, \
michael@0 221 E ## isInvalid4
michael@0 222
michael@0 223 static int FASTCALL checkCharRefNumber(int);
michael@0 224
michael@0 225 #include "xmltok_impl.h"
michael@0 226 #include "ascii.h"
michael@0 227
michael@0 228 #ifdef XML_MIN_SIZE
michael@0 229 #define sb_isNameMin isNever
michael@0 230 #define sb_isNmstrtMin isNever
michael@0 231 #endif
michael@0 232
michael@0 233 #ifdef XML_MIN_SIZE
michael@0 234 #define MINBPC(enc) ((enc)->minBytesPerChar)
michael@0 235 #else
michael@0 236 /* minimum bytes per character */
michael@0 237 #define MINBPC(enc) 1
michael@0 238 #endif
michael@0 239
michael@0 240 #define SB_BYTE_TYPE(enc, p) \
michael@0 241 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
michael@0 242
michael@0 243 #ifdef XML_MIN_SIZE
michael@0 244 static int PTRFASTCALL
michael@0 245 sb_byteType(const ENCODING *enc, const char *p)
michael@0 246 {
michael@0 247 return SB_BYTE_TYPE(enc, p);
michael@0 248 }
michael@0 249 #define BYTE_TYPE(enc, p) \
michael@0 250 (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
michael@0 251 #else
michael@0 252 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
michael@0 253 #endif
michael@0 254
michael@0 255 #ifdef XML_MIN_SIZE
michael@0 256 #define BYTE_TO_ASCII(enc, p) \
michael@0 257 (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
michael@0 258 static int PTRFASTCALL
michael@0 259 sb_byteToAscii(const ENCODING *enc, const char *p)
michael@0 260 {
michael@0 261 return *p;
michael@0 262 }
michael@0 263 #else
michael@0 264 #define BYTE_TO_ASCII(enc, p) (*(p))
michael@0 265 #endif
michael@0 266
michael@0 267 #define IS_NAME_CHAR(enc, p, n) \
michael@0 268 (AS_NORMAL_ENCODING(enc)->isName ## n(enc, p))
michael@0 269 #define IS_NMSTRT_CHAR(enc, p, n) \
michael@0 270 (AS_NORMAL_ENCODING(enc)->isNmstrt ## n(enc, p))
michael@0 271 #define IS_INVALID_CHAR(enc, p, n) \
michael@0 272 (AS_NORMAL_ENCODING(enc)->isInvalid ## n(enc, p))
michael@0 273
michael@0 274 #ifdef XML_MIN_SIZE
michael@0 275 #define IS_NAME_CHAR_MINBPC(enc, p) \
michael@0 276 (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
michael@0 277 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
michael@0 278 (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
michael@0 279 #else
michael@0 280 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
michael@0 281 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
michael@0 282 #endif
michael@0 283
michael@0 284 #ifdef XML_MIN_SIZE
michael@0 285 #define CHAR_MATCHES(enc, p, c) \
michael@0 286 (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
michael@0 287 static int PTRCALL
michael@0 288 sb_charMatches(const ENCODING *enc, const char *p, int c)
michael@0 289 {
michael@0 290 return *p == c;
michael@0 291 }
michael@0 292 #else
michael@0 293 /* c is an ASCII character */
michael@0 294 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
michael@0 295 #endif
michael@0 296
michael@0 297 #define PREFIX(ident) normal_ ## ident
michael@0 298 #include "xmltok_impl.c"
michael@0 299
michael@0 300 #undef MINBPC
michael@0 301 #undef BYTE_TYPE
michael@0 302 #undef BYTE_TO_ASCII
michael@0 303 #undef CHAR_MATCHES
michael@0 304 #undef IS_NAME_CHAR
michael@0 305 #undef IS_NAME_CHAR_MINBPC
michael@0 306 #undef IS_NMSTRT_CHAR
michael@0 307 #undef IS_NMSTRT_CHAR_MINBPC
michael@0 308 #undef IS_INVALID_CHAR
michael@0 309
michael@0 310 enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
michael@0 311 UTF8_cval1 = 0x00,
michael@0 312 UTF8_cval2 = 0xc0,
michael@0 313 UTF8_cval3 = 0xe0,
michael@0 314 UTF8_cval4 = 0xf0
michael@0 315 };
michael@0 316
michael@0 317 static void PTRCALL
michael@0 318 utf8_toUtf8(const ENCODING *enc,
michael@0 319 const char **fromP, const char *fromLim,
michael@0 320 char **toP, const char *toLim)
michael@0 321 {
michael@0 322 char *to;
michael@0 323 const char *from;
michael@0 324 if (fromLim - *fromP > toLim - *toP) {
michael@0 325 /* Avoid copying partial characters. */
michael@0 326 for (fromLim = *fromP + (toLim - *toP); fromLim > *fromP; fromLim--)
michael@0 327 if (((unsigned char)fromLim[-1] & 0xc0) != 0x80)
michael@0 328 break;
michael@0 329 }
michael@0 330 for (to = *toP, from = *fromP; from != fromLim; from++, to++)
michael@0 331 *to = *from;
michael@0 332 *fromP = from;
michael@0 333 *toP = to;
michael@0 334 }
michael@0 335
michael@0 336 static void PTRCALL
michael@0 337 utf8_toUtf16(const ENCODING *enc,
michael@0 338 const char **fromP, const char *fromLim,
michael@0 339 unsigned short **toP, const unsigned short *toLim)
michael@0 340 {
michael@0 341 unsigned short *to = *toP;
michael@0 342 const char *from = *fromP;
michael@0 343 while (from != fromLim && to != toLim) {
michael@0 344 switch (((struct normal_encoding *)enc)->type[(unsigned char)*from]) {
michael@0 345 case BT_LEAD2:
michael@0 346 *to++ = (unsigned short)(((from[0] & 0x1f) << 6) | (from[1] & 0x3f));
michael@0 347 from += 2;
michael@0 348 break;
michael@0 349 case BT_LEAD3:
michael@0 350 *to++ = (unsigned short)(((from[0] & 0xf) << 12)
michael@0 351 | ((from[1] & 0x3f) << 6) | (from[2] & 0x3f));
michael@0 352 from += 3;
michael@0 353 break;
michael@0 354 case BT_LEAD4:
michael@0 355 {
michael@0 356 unsigned long n;
michael@0 357 if (to + 1 == toLim)
michael@0 358 goto after;
michael@0 359 n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12)
michael@0 360 | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
michael@0 361 n -= 0x10000;
michael@0 362 to[0] = (unsigned short)((n >> 10) | 0xD800);
michael@0 363 to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
michael@0 364 to += 2;
michael@0 365 from += 4;
michael@0 366 }
michael@0 367 break;
michael@0 368 default:
michael@0 369 *to++ = *from++;
michael@0 370 break;
michael@0 371 }
michael@0 372 }
michael@0 373 after:
michael@0 374 *fromP = from;
michael@0 375 *toP = to;
michael@0 376 }
michael@0 377
michael@0 378 #ifdef XML_NS
michael@0 379 static const struct normal_encoding utf8_encoding_ns = {
michael@0 380 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
michael@0 381 {
michael@0 382 #include "asciitab.h"
michael@0 383 #include "utf8tab.h"
michael@0 384 },
michael@0 385 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
michael@0 386 };
michael@0 387 #endif
michael@0 388
michael@0 389 static const struct normal_encoding utf8_encoding = {
michael@0 390 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
michael@0 391 {
michael@0 392 #define BT_COLON BT_NMSTRT
michael@0 393 #include "asciitab.h"
michael@0 394 #undef BT_COLON
michael@0 395 #include "utf8tab.h"
michael@0 396 },
michael@0 397 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
michael@0 398 };
michael@0 399
michael@0 400 #ifdef XML_NS
michael@0 401
michael@0 402 static const struct normal_encoding internal_utf8_encoding_ns = {
michael@0 403 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
michael@0 404 {
michael@0 405 #include "iasciitab.h"
michael@0 406 #include "utf8tab.h"
michael@0 407 },
michael@0 408 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
michael@0 409 };
michael@0 410
michael@0 411 #endif
michael@0 412
michael@0 413 static const struct normal_encoding internal_utf8_encoding = {
michael@0 414 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
michael@0 415 {
michael@0 416 #define BT_COLON BT_NMSTRT
michael@0 417 #include "iasciitab.h"
michael@0 418 #undef BT_COLON
michael@0 419 #include "utf8tab.h"
michael@0 420 },
michael@0 421 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
michael@0 422 };
michael@0 423
michael@0 424 static void PTRCALL
michael@0 425 latin1_toUtf8(const ENCODING *enc,
michael@0 426 const char **fromP, const char *fromLim,
michael@0 427 char **toP, const char *toLim)
michael@0 428 {
michael@0 429 for (;;) {
michael@0 430 unsigned char c;
michael@0 431 if (*fromP == fromLim)
michael@0 432 break;
michael@0 433 c = (unsigned char)**fromP;
michael@0 434 if (c & 0x80) {
michael@0 435 if (toLim - *toP < 2)
michael@0 436 break;
michael@0 437 *(*toP)++ = (char)((c >> 6) | UTF8_cval2);
michael@0 438 *(*toP)++ = (char)((c & 0x3f) | 0x80);
michael@0 439 (*fromP)++;
michael@0 440 }
michael@0 441 else {
michael@0 442 if (*toP == toLim)
michael@0 443 break;
michael@0 444 *(*toP)++ = *(*fromP)++;
michael@0 445 }
michael@0 446 }
michael@0 447 }
michael@0 448
michael@0 449 static void PTRCALL
michael@0 450 latin1_toUtf16(const ENCODING *enc,
michael@0 451 const char **fromP, const char *fromLim,
michael@0 452 unsigned short **toP, const unsigned short *toLim)
michael@0 453 {
michael@0 454 while (*fromP != fromLim && *toP != toLim)
michael@0 455 *(*toP)++ = (unsigned char)*(*fromP)++;
michael@0 456 }
michael@0 457
michael@0 458 #ifdef XML_NS
michael@0 459
michael@0 460 static const struct normal_encoding latin1_encoding_ns = {
michael@0 461 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
michael@0 462 {
michael@0 463 #include "asciitab.h"
michael@0 464 #include "latin1tab.h"
michael@0 465 },
michael@0 466 STANDARD_VTABLE(sb_)
michael@0 467 };
michael@0 468
michael@0 469 #endif
michael@0 470
michael@0 471 static const struct normal_encoding latin1_encoding = {
michael@0 472 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
michael@0 473 {
michael@0 474 #define BT_COLON BT_NMSTRT
michael@0 475 #include "asciitab.h"
michael@0 476 #undef BT_COLON
michael@0 477 #include "latin1tab.h"
michael@0 478 },
michael@0 479 STANDARD_VTABLE(sb_)
michael@0 480 };
michael@0 481
michael@0 482 static void PTRCALL
michael@0 483 ascii_toUtf8(const ENCODING *enc,
michael@0 484 const char **fromP, const char *fromLim,
michael@0 485 char **toP, const char *toLim)
michael@0 486 {
michael@0 487 while (*fromP != fromLim && *toP != toLim)
michael@0 488 *(*toP)++ = *(*fromP)++;
michael@0 489 }
michael@0 490
michael@0 491 #ifdef XML_NS
michael@0 492
michael@0 493 static const struct normal_encoding ascii_encoding_ns = {
michael@0 494 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
michael@0 495 {
michael@0 496 #include "asciitab.h"
michael@0 497 /* BT_NONXML == 0 */
michael@0 498 },
michael@0 499 STANDARD_VTABLE(sb_)
michael@0 500 };
michael@0 501
michael@0 502 #endif
michael@0 503
michael@0 504 static const struct normal_encoding ascii_encoding = {
michael@0 505 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
michael@0 506 {
michael@0 507 #define BT_COLON BT_NMSTRT
michael@0 508 #include "asciitab.h"
michael@0 509 #undef BT_COLON
michael@0 510 /* BT_NONXML == 0 */
michael@0 511 },
michael@0 512 STANDARD_VTABLE(sb_)
michael@0 513 };
michael@0 514
michael@0 515 static int PTRFASTCALL
michael@0 516 unicode_byte_type(char hi, char lo)
michael@0 517 {
michael@0 518 switch ((unsigned char)hi) {
michael@0 519 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
michael@0 520 return BT_LEAD4;
michael@0 521 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
michael@0 522 return BT_TRAIL;
michael@0 523 case 0xFF:
michael@0 524 switch ((unsigned char)lo) {
michael@0 525 case 0xFF:
michael@0 526 case 0xFE:
michael@0 527 return BT_NONXML;
michael@0 528 }
michael@0 529 break;
michael@0 530 }
michael@0 531 return BT_NONASCII;
michael@0 532 }
michael@0 533
michael@0 534 #define DEFINE_UTF16_TO_UTF8(E) \
michael@0 535 static void PTRCALL \
michael@0 536 E ## toUtf8(const ENCODING *enc, \
michael@0 537 const char **fromP, const char *fromLim, \
michael@0 538 char **toP, const char *toLim) \
michael@0 539 { \
michael@0 540 const char *from; \
michael@0 541 for (from = *fromP; from != fromLim; from += 2) { \
michael@0 542 int plane; \
michael@0 543 unsigned char lo2; \
michael@0 544 unsigned char lo = GET_LO(from); \
michael@0 545 unsigned char hi = GET_HI(from); \
michael@0 546 switch (hi) { \
michael@0 547 case 0: \
michael@0 548 if (lo < 0x80) { \
michael@0 549 if (*toP == toLim) { \
michael@0 550 *fromP = from; \
michael@0 551 return; \
michael@0 552 } \
michael@0 553 *(*toP)++ = lo; \
michael@0 554 break; \
michael@0 555 } \
michael@0 556 /* fall through */ \
michael@0 557 case 0x1: case 0x2: case 0x3: \
michael@0 558 case 0x4: case 0x5: case 0x6: case 0x7: \
michael@0 559 if (toLim - *toP < 2) { \
michael@0 560 *fromP = from; \
michael@0 561 return; \
michael@0 562 } \
michael@0 563 *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
michael@0 564 *(*toP)++ = ((lo & 0x3f) | 0x80); \
michael@0 565 break; \
michael@0 566 default: \
michael@0 567 if (toLim - *toP < 3) { \
michael@0 568 *fromP = from; \
michael@0 569 return; \
michael@0 570 } \
michael@0 571 /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
michael@0 572 *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
michael@0 573 *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
michael@0 574 *(*toP)++ = ((lo & 0x3f) | 0x80); \
michael@0 575 break; \
michael@0 576 case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
michael@0 577 if (toLim - *toP < 4) { \
michael@0 578 *fromP = from; \
michael@0 579 return; \
michael@0 580 } \
michael@0 581 plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
michael@0 582 *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
michael@0 583 *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
michael@0 584 from += 2; \
michael@0 585 lo2 = GET_LO(from); \
michael@0 586 *(*toP)++ = (((lo & 0x3) << 4) \
michael@0 587 | ((GET_HI(from) & 0x3) << 2) \
michael@0 588 | (lo2 >> 6) \
michael@0 589 | 0x80); \
michael@0 590 *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
michael@0 591 break; \
michael@0 592 } \
michael@0 593 } \
michael@0 594 *fromP = from; \
michael@0 595 }
michael@0 596
michael@0 597 #define DEFINE_UTF16_TO_UTF16(E) \
michael@0 598 static void PTRCALL \
michael@0 599 E ## toUtf16(const ENCODING *enc, \
michael@0 600 const char **fromP, const char *fromLim, \
michael@0 601 unsigned short **toP, const unsigned short *toLim) \
michael@0 602 { \
michael@0 603 /* Avoid copying first half only of surrogate */ \
michael@0 604 if (fromLim - *fromP > ((toLim - *toP) << 1) \
michael@0 605 && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
michael@0 606 fromLim -= 2; \
michael@0 607 for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
michael@0 608 *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
michael@0 609 }
michael@0 610
michael@0 611 #define SET2(ptr, ch) \
michael@0 612 (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
michael@0 613 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
michael@0 614 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
michael@0 615
michael@0 616 DEFINE_UTF16_TO_UTF8(little2_)
michael@0 617 DEFINE_UTF16_TO_UTF16(little2_)
michael@0 618
michael@0 619 #undef SET2
michael@0 620 #undef GET_LO
michael@0 621 #undef GET_HI
michael@0 622
michael@0 623 #define SET2(ptr, ch) \
michael@0 624 (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
michael@0 625 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
michael@0 626 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
michael@0 627
michael@0 628 DEFINE_UTF16_TO_UTF8(big2_)
michael@0 629 DEFINE_UTF16_TO_UTF16(big2_)
michael@0 630
michael@0 631 #undef SET2
michael@0 632 #undef GET_LO
michael@0 633 #undef GET_HI
michael@0 634
michael@0 635 #define LITTLE2_BYTE_TYPE(enc, p) \
michael@0 636 ((p)[1] == 0 \
michael@0 637 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
michael@0 638 : unicode_byte_type((p)[1], (p)[0]))
michael@0 639 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
michael@0 640 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
michael@0 641 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
michael@0 642 UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
michael@0 643 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
michael@0 644 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
michael@0 645
michael@0 646 #ifdef XML_MIN_SIZE
michael@0 647
michael@0 648 static int PTRFASTCALL
michael@0 649 little2_byteType(const ENCODING *enc, const char *p)
michael@0 650 {
michael@0 651 return LITTLE2_BYTE_TYPE(enc, p);
michael@0 652 }
michael@0 653
michael@0 654 static int PTRFASTCALL
michael@0 655 little2_byteToAscii(const ENCODING *enc, const char *p)
michael@0 656 {
michael@0 657 return LITTLE2_BYTE_TO_ASCII(enc, p);
michael@0 658 }
michael@0 659
michael@0 660 static int PTRCALL
michael@0 661 little2_charMatches(const ENCODING *enc, const char *p, int c)
michael@0 662 {
michael@0 663 return LITTLE2_CHAR_MATCHES(enc, p, c);
michael@0 664 }
michael@0 665
michael@0 666 static int PTRFASTCALL
michael@0 667 little2_isNameMin(const ENCODING *enc, const char *p)
michael@0 668 {
michael@0 669 return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
michael@0 670 }
michael@0 671
michael@0 672 static int PTRFASTCALL
michael@0 673 little2_isNmstrtMin(const ENCODING *enc, const char *p)
michael@0 674 {
michael@0 675 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
michael@0 676 }
michael@0 677
michael@0 678 #undef VTABLE
michael@0 679 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
michael@0 680
michael@0 681 #else /* not XML_MIN_SIZE */
michael@0 682
michael@0 683 #undef PREFIX
michael@0 684 #define PREFIX(ident) little2_ ## ident
michael@0 685 #define MINBPC(enc) 2
michael@0 686 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
michael@0 687 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
michael@0 688 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
michael@0 689 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
michael@0 690 #define IS_NAME_CHAR(enc, p, n) 0
michael@0 691 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
michael@0 692 #define IS_NMSTRT_CHAR(enc, p, n) (0)
michael@0 693 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
michael@0 694
michael@0 695 #include "xmltok_impl.c"
michael@0 696
michael@0 697 #undef MINBPC
michael@0 698 #undef BYTE_TYPE
michael@0 699 #undef BYTE_TO_ASCII
michael@0 700 #undef CHAR_MATCHES
michael@0 701 #undef IS_NAME_CHAR
michael@0 702 #undef IS_NAME_CHAR_MINBPC
michael@0 703 #undef IS_NMSTRT_CHAR
michael@0 704 #undef IS_NMSTRT_CHAR_MINBPC
michael@0 705 #undef IS_INVALID_CHAR
michael@0 706
michael@0 707 #endif /* not XML_MIN_SIZE */
michael@0 708
michael@0 709 #ifdef XML_NS
michael@0 710
michael@0 711 static const struct normal_encoding little2_encoding_ns = {
michael@0 712 { VTABLE, 2, 0,
michael@0 713 #if BYTEORDER == 1234
michael@0 714 1
michael@0 715 #else
michael@0 716 0
michael@0 717 #endif
michael@0 718 },
michael@0 719 {
michael@0 720 #include "asciitab.h"
michael@0 721 #include "latin1tab.h"
michael@0 722 },
michael@0 723 STANDARD_VTABLE(little2_)
michael@0 724 };
michael@0 725
michael@0 726 #endif
michael@0 727
michael@0 728 static const struct normal_encoding little2_encoding = {
michael@0 729 { VTABLE, 2, 0,
michael@0 730 #if BYTEORDER == 1234
michael@0 731 1
michael@0 732 #else
michael@0 733 0
michael@0 734 #endif
michael@0 735 },
michael@0 736 {
michael@0 737 #define BT_COLON BT_NMSTRT
michael@0 738 #include "asciitab.h"
michael@0 739 #undef BT_COLON
michael@0 740 #include "latin1tab.h"
michael@0 741 },
michael@0 742 STANDARD_VTABLE(little2_)
michael@0 743 };
michael@0 744
michael@0 745 #if BYTEORDER != 4321
michael@0 746
michael@0 747 #ifdef XML_NS
michael@0 748
michael@0 749 static const struct normal_encoding internal_little2_encoding_ns = {
michael@0 750 { VTABLE, 2, 0, 1 },
michael@0 751 {
michael@0 752 #include "iasciitab.h"
michael@0 753 #include "latin1tab.h"
michael@0 754 },
michael@0 755 STANDARD_VTABLE(little2_)
michael@0 756 };
michael@0 757
michael@0 758 #endif
michael@0 759
michael@0 760 static const struct normal_encoding internal_little2_encoding = {
michael@0 761 { VTABLE, 2, 0, 1 },
michael@0 762 {
michael@0 763 #define BT_COLON BT_NMSTRT
michael@0 764 #include "iasciitab.h"
michael@0 765 #undef BT_COLON
michael@0 766 #include "latin1tab.h"
michael@0 767 },
michael@0 768 STANDARD_VTABLE(little2_)
michael@0 769 };
michael@0 770
michael@0 771 #endif
michael@0 772
michael@0 773
michael@0 774 #define BIG2_BYTE_TYPE(enc, p) \
michael@0 775 ((p)[0] == 0 \
michael@0 776 ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
michael@0 777 : unicode_byte_type((p)[0], (p)[1]))
michael@0 778 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
michael@0 779 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
michael@0 780 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
michael@0 781 UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
michael@0 782 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
michael@0 783 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
michael@0 784
michael@0 785 #ifdef XML_MIN_SIZE
michael@0 786
michael@0 787 static int PTRFASTCALL
michael@0 788 big2_byteType(const ENCODING *enc, const char *p)
michael@0 789 {
michael@0 790 return BIG2_BYTE_TYPE(enc, p);
michael@0 791 }
michael@0 792
michael@0 793 static int PTRFASTCALL
michael@0 794 big2_byteToAscii(const ENCODING *enc, const char *p)
michael@0 795 {
michael@0 796 return BIG2_BYTE_TO_ASCII(enc, p);
michael@0 797 }
michael@0 798
michael@0 799 static int PTRCALL
michael@0 800 big2_charMatches(const ENCODING *enc, const char *p, int c)
michael@0 801 {
michael@0 802 return BIG2_CHAR_MATCHES(enc, p, c);
michael@0 803 }
michael@0 804
michael@0 805 static int PTRFASTCALL
michael@0 806 big2_isNameMin(const ENCODING *enc, const char *p)
michael@0 807 {
michael@0 808 return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
michael@0 809 }
michael@0 810
michael@0 811 static int PTRFASTCALL
michael@0 812 big2_isNmstrtMin(const ENCODING *enc, const char *p)
michael@0 813 {
michael@0 814 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
michael@0 815 }
michael@0 816
michael@0 817 #undef VTABLE
michael@0 818 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
michael@0 819
michael@0 820 #else /* not XML_MIN_SIZE */
michael@0 821
michael@0 822 #undef PREFIX
michael@0 823 #define PREFIX(ident) big2_ ## ident
michael@0 824 #define MINBPC(enc) 2
michael@0 825 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
michael@0 826 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
michael@0 827 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
michael@0 828 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
michael@0 829 #define IS_NAME_CHAR(enc, p, n) 0
michael@0 830 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
michael@0 831 #define IS_NMSTRT_CHAR(enc, p, n) (0)
michael@0 832 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
michael@0 833
michael@0 834 #include "xmltok_impl.c"
michael@0 835
michael@0 836 #undef MINBPC
michael@0 837 #undef BYTE_TYPE
michael@0 838 #undef BYTE_TO_ASCII
michael@0 839 #undef CHAR_MATCHES
michael@0 840 #undef IS_NAME_CHAR
michael@0 841 #undef IS_NAME_CHAR_MINBPC
michael@0 842 #undef IS_NMSTRT_CHAR
michael@0 843 #undef IS_NMSTRT_CHAR_MINBPC
michael@0 844 #undef IS_INVALID_CHAR
michael@0 845
michael@0 846 #endif /* not XML_MIN_SIZE */
michael@0 847
michael@0 848 #ifdef XML_NS
michael@0 849
michael@0 850 static const struct normal_encoding big2_encoding_ns = {
michael@0 851 { VTABLE, 2, 0,
michael@0 852 #if BYTEORDER == 4321
michael@0 853 1
michael@0 854 #else
michael@0 855 0
michael@0 856 #endif
michael@0 857 },
michael@0 858 {
michael@0 859 #include "asciitab.h"
michael@0 860 #include "latin1tab.h"
michael@0 861 },
michael@0 862 STANDARD_VTABLE(big2_)
michael@0 863 };
michael@0 864
michael@0 865 #endif
michael@0 866
michael@0 867 static const struct normal_encoding big2_encoding = {
michael@0 868 { VTABLE, 2, 0,
michael@0 869 #if BYTEORDER == 4321
michael@0 870 1
michael@0 871 #else
michael@0 872 0
michael@0 873 #endif
michael@0 874 },
michael@0 875 {
michael@0 876 #define BT_COLON BT_NMSTRT
michael@0 877 #include "asciitab.h"
michael@0 878 #undef BT_COLON
michael@0 879 #include "latin1tab.h"
michael@0 880 },
michael@0 881 STANDARD_VTABLE(big2_)
michael@0 882 };
michael@0 883
michael@0 884 #if BYTEORDER != 1234
michael@0 885
michael@0 886 #ifdef XML_NS
michael@0 887
michael@0 888 static const struct normal_encoding internal_big2_encoding_ns = {
michael@0 889 { VTABLE, 2, 0, 1 },
michael@0 890 {
michael@0 891 #include "iasciitab.h"
michael@0 892 #include "latin1tab.h"
michael@0 893 },
michael@0 894 STANDARD_VTABLE(big2_)
michael@0 895 };
michael@0 896
michael@0 897 #endif
michael@0 898
michael@0 899 static const struct normal_encoding internal_big2_encoding = {
michael@0 900 { VTABLE, 2, 0, 1 },
michael@0 901 {
michael@0 902 #define BT_COLON BT_NMSTRT
michael@0 903 #include "iasciitab.h"
michael@0 904 #undef BT_COLON
michael@0 905 #include "latin1tab.h"
michael@0 906 },
michael@0 907 STANDARD_VTABLE(big2_)
michael@0 908 };
michael@0 909
michael@0 910 #endif
michael@0 911
michael@0 912 #undef PREFIX
michael@0 913
michael@0 914 static int FASTCALL
michael@0 915 streqci(const char *s1, const char *s2)
michael@0 916 {
michael@0 917 for (;;) {
michael@0 918 char c1 = *s1++;
michael@0 919 char c2 = *s2++;
michael@0 920 if (ASCII_a <= c1 && c1 <= ASCII_z)
michael@0 921 c1 += ASCII_A - ASCII_a;
michael@0 922 if (ASCII_a <= c2 && c2 <= ASCII_z)
michael@0 923 c2 += ASCII_A - ASCII_a;
michael@0 924 if (c1 != c2)
michael@0 925 return 0;
michael@0 926 if (!c1)
michael@0 927 break;
michael@0 928 }
michael@0 929 return 1;
michael@0 930 }
michael@0 931
michael@0 932 static void PTRCALL
michael@0 933 initUpdatePosition(const ENCODING *enc, const char *ptr,
michael@0 934 const char *end, POSITION *pos)
michael@0 935 {
michael@0 936 normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
michael@0 937 }
michael@0 938
michael@0 939 static int
michael@0 940 toAscii(const ENCODING *enc, const char *ptr, const char *end)
michael@0 941 {
michael@0 942 char buf[1];
michael@0 943 char *p = buf;
michael@0 944 XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
michael@0 945 if (p == buf)
michael@0 946 return -1;
michael@0 947 else
michael@0 948 return buf[0];
michael@0 949 }
michael@0 950
michael@0 951 static int FASTCALL
michael@0 952 isSpace(int c)
michael@0 953 {
michael@0 954 switch (c) {
michael@0 955 case 0x20:
michael@0 956 case 0xD:
michael@0 957 case 0xA:
michael@0 958 case 0x9:
michael@0 959 return 1;
michael@0 960 }
michael@0 961 return 0;
michael@0 962 }
michael@0 963
michael@0 964 /* Return 1 if there's just optional white space or there's an S
michael@0 965 followed by name=val.
michael@0 966 */
michael@0 967 static int
michael@0 968 parsePseudoAttribute(const ENCODING *enc,
michael@0 969 const char *ptr,
michael@0 970 const char *end,
michael@0 971 const char **namePtr,
michael@0 972 const char **nameEndPtr,
michael@0 973 const char **valPtr,
michael@0 974 const char **nextTokPtr)
michael@0 975 {
michael@0 976 int c;
michael@0 977 char open;
michael@0 978 if (ptr == end) {
michael@0 979 *namePtr = NULL;
michael@0 980 return 1;
michael@0 981 }
michael@0 982 if (!isSpace(toAscii(enc, ptr, end))) {
michael@0 983 *nextTokPtr = ptr;
michael@0 984 return 0;
michael@0 985 }
michael@0 986 do {
michael@0 987 ptr += enc->minBytesPerChar;
michael@0 988 } while (isSpace(toAscii(enc, ptr, end)));
michael@0 989 if (ptr == end) {
michael@0 990 *namePtr = NULL;
michael@0 991 return 1;
michael@0 992 }
michael@0 993 *namePtr = ptr;
michael@0 994 for (;;) {
michael@0 995 c = toAscii(enc, ptr, end);
michael@0 996 if (c == -1) {
michael@0 997 *nextTokPtr = ptr;
michael@0 998 return 0;
michael@0 999 }
michael@0 1000 if (c == ASCII_EQUALS) {
michael@0 1001 *nameEndPtr = ptr;
michael@0 1002 break;
michael@0 1003 }
michael@0 1004 if (isSpace(c)) {
michael@0 1005 *nameEndPtr = ptr;
michael@0 1006 do {
michael@0 1007 ptr += enc->minBytesPerChar;
michael@0 1008 } while (isSpace(c = toAscii(enc, ptr, end)));
michael@0 1009 if (c != ASCII_EQUALS) {
michael@0 1010 *nextTokPtr = ptr;
michael@0 1011 return 0;
michael@0 1012 }
michael@0 1013 break;
michael@0 1014 }
michael@0 1015 ptr += enc->minBytesPerChar;
michael@0 1016 }
michael@0 1017 if (ptr == *namePtr) {
michael@0 1018 *nextTokPtr = ptr;
michael@0 1019 return 0;
michael@0 1020 }
michael@0 1021 ptr += enc->minBytesPerChar;
michael@0 1022 c = toAscii(enc, ptr, end);
michael@0 1023 while (isSpace(c)) {
michael@0 1024 ptr += enc->minBytesPerChar;
michael@0 1025 c = toAscii(enc, ptr, end);
michael@0 1026 }
michael@0 1027 if (c != ASCII_QUOT && c != ASCII_APOS) {
michael@0 1028 *nextTokPtr = ptr;
michael@0 1029 return 0;
michael@0 1030 }
michael@0 1031 open = (char)c;
michael@0 1032 ptr += enc->minBytesPerChar;
michael@0 1033 *valPtr = ptr;
michael@0 1034 for (;; ptr += enc->minBytesPerChar) {
michael@0 1035 c = toAscii(enc, ptr, end);
michael@0 1036 if (c == open)
michael@0 1037 break;
michael@0 1038 if (!(ASCII_a <= c && c <= ASCII_z)
michael@0 1039 && !(ASCII_A <= c && c <= ASCII_Z)
michael@0 1040 && !(ASCII_0 <= c && c <= ASCII_9)
michael@0 1041 && c != ASCII_PERIOD
michael@0 1042 && c != ASCII_MINUS
michael@0 1043 && c != ASCII_UNDERSCORE) {
michael@0 1044 *nextTokPtr = ptr;
michael@0 1045 return 0;
michael@0 1046 }
michael@0 1047 }
michael@0 1048 *nextTokPtr = ptr + enc->minBytesPerChar;
michael@0 1049 return 1;
michael@0 1050 }
michael@0 1051
michael@0 1052 static const char KW_version[] = {
michael@0 1053 ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
michael@0 1054 };
michael@0 1055
michael@0 1056 static const char KW_encoding[] = {
michael@0 1057 ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
michael@0 1058 };
michael@0 1059
michael@0 1060 static const char KW_standalone[] = {
michael@0 1061 ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a, ASCII_l, ASCII_o,
michael@0 1062 ASCII_n, ASCII_e, '\0'
michael@0 1063 };
michael@0 1064
michael@0 1065 static const char KW_yes[] = {
michael@0 1066 ASCII_y, ASCII_e, ASCII_s, '\0'
michael@0 1067 };
michael@0 1068
michael@0 1069 static const char KW_no[] = {
michael@0 1070 ASCII_n, ASCII_o, '\0'
michael@0 1071 };
michael@0 1072
michael@0 1073 /* BEGIN MOZILLA CHANGE (http://bugzilla.mozilla.org/show_bug.cgi?id=62157) */
michael@0 1074 static const char KW_XML_1_0[] = {
michael@0 1075 ASCII_1, ASCII_PERIOD, ASCII_0, '\0'
michael@0 1076 };
michael@0 1077 /* END MOZILLA CHANGE */
michael@0 1078
michael@0 1079 static int
michael@0 1080 doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *,
michael@0 1081 const char *,
michael@0 1082 const char *),
michael@0 1083 int isGeneralTextEntity,
michael@0 1084 const ENCODING *enc,
michael@0 1085 const char *ptr,
michael@0 1086 const char *end,
michael@0 1087 const char **badPtr,
michael@0 1088 const char **versionPtr,
michael@0 1089 const char **versionEndPtr,
michael@0 1090 const char **encodingName,
michael@0 1091 const ENCODING **encoding,
michael@0 1092 int *standalone)
michael@0 1093 {
michael@0 1094 const char *val = NULL;
michael@0 1095 const char *name = NULL;
michael@0 1096 const char *nameEnd = NULL;
michael@0 1097 ptr += 5 * enc->minBytesPerChar;
michael@0 1098 end -= 2 * enc->minBytesPerChar;
michael@0 1099 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)
michael@0 1100 || !name) {
michael@0 1101 *badPtr = ptr;
michael@0 1102 return 0;
michael@0 1103 }
michael@0 1104 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
michael@0 1105 if (!isGeneralTextEntity) {
michael@0 1106 *badPtr = name;
michael@0 1107 return 0;
michael@0 1108 }
michael@0 1109 }
michael@0 1110 else {
michael@0 1111 if (versionPtr)
michael@0 1112 *versionPtr = val;
michael@0 1113 if (versionEndPtr)
michael@0 1114 *versionEndPtr = ptr;
michael@0 1115 /* BEGIN MOZILLA CHANGE (http://bugzilla.mozilla.org/show_bug.cgi?id=62157) */
michael@0 1116 /* Anything else but a version="1.0" is invalid for us, until we support later versions. */
michael@0 1117 if (!XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_XML_1_0)) {
michael@0 1118 *badPtr = val;
michael@0 1119 return 0;
michael@0 1120 }
michael@0 1121 /* END MOZILLA CHANGE */
michael@0 1122 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
michael@0 1123 *badPtr = ptr;
michael@0 1124 return 0;
michael@0 1125 }
michael@0 1126 if (!name) {
michael@0 1127 if (isGeneralTextEntity) {
michael@0 1128 /* a TextDecl must have an EncodingDecl */
michael@0 1129 *badPtr = ptr;
michael@0 1130 return 0;
michael@0 1131 }
michael@0 1132 return 1;
michael@0 1133 }
michael@0 1134 }
michael@0 1135 if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
michael@0 1136 int c = toAscii(enc, val, end);
michael@0 1137 if (!(ASCII_a <= c && c <= ASCII_z) && !(ASCII_A <= c && c <= ASCII_Z)) {
michael@0 1138 *badPtr = val;
michael@0 1139 return 0;
michael@0 1140 }
michael@0 1141 if (encodingName)
michael@0 1142 *encodingName = val;
michael@0 1143 if (encoding)
michael@0 1144 *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
michael@0 1145 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
michael@0 1146 *badPtr = ptr;
michael@0 1147 return 0;
michael@0 1148 }
michael@0 1149 if (!name)
michael@0 1150 return 1;
michael@0 1151 }
michael@0 1152 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone)
michael@0 1153 || isGeneralTextEntity) {
michael@0 1154 *badPtr = name;
michael@0 1155 return 0;
michael@0 1156 }
michael@0 1157 if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
michael@0 1158 if (standalone)
michael@0 1159 *standalone = 1;
michael@0 1160 }
michael@0 1161 else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
michael@0 1162 if (standalone)
michael@0 1163 *standalone = 0;
michael@0 1164 }
michael@0 1165 else {
michael@0 1166 *badPtr = val;
michael@0 1167 return 0;
michael@0 1168 }
michael@0 1169 while (isSpace(toAscii(enc, ptr, end)))
michael@0 1170 ptr += enc->minBytesPerChar;
michael@0 1171 if (ptr != end) {
michael@0 1172 *badPtr = ptr;
michael@0 1173 return 0;
michael@0 1174 }
michael@0 1175 return 1;
michael@0 1176 }
michael@0 1177
michael@0 1178 static int FASTCALL
michael@0 1179 checkCharRefNumber(int result)
michael@0 1180 {
michael@0 1181 switch (result >> 8) {
michael@0 1182 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
michael@0 1183 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
michael@0 1184 return -1;
michael@0 1185 case 0:
michael@0 1186 if (latin1_encoding.type[result] == BT_NONXML)
michael@0 1187 return -1;
michael@0 1188 break;
michael@0 1189 case 0xFF:
michael@0 1190 if (result == 0xFFFE || result == 0xFFFF)
michael@0 1191 return -1;
michael@0 1192 break;
michael@0 1193 }
michael@0 1194 return result;
michael@0 1195 }
michael@0 1196
michael@0 1197 int FASTCALL
michael@0 1198 XmlUtf8Encode(int c, char *buf)
michael@0 1199 {
michael@0 1200 enum {
michael@0 1201 /* minN is minimum legal resulting value for N byte sequence */
michael@0 1202 min2 = 0x80,
michael@0 1203 min3 = 0x800,
michael@0 1204 min4 = 0x10000
michael@0 1205 };
michael@0 1206
michael@0 1207 if (c < 0)
michael@0 1208 return 0;
michael@0 1209 if (c < min2) {
michael@0 1210 buf[0] = (char)(c | UTF8_cval1);
michael@0 1211 return 1;
michael@0 1212 }
michael@0 1213 if (c < min3) {
michael@0 1214 buf[0] = (char)((c >> 6) | UTF8_cval2);
michael@0 1215 buf[1] = (char)((c & 0x3f) | 0x80);
michael@0 1216 return 2;
michael@0 1217 }
michael@0 1218 if (c < min4) {
michael@0 1219 buf[0] = (char)((c >> 12) | UTF8_cval3);
michael@0 1220 buf[1] = (char)(((c >> 6) & 0x3f) | 0x80);
michael@0 1221 buf[2] = (char)((c & 0x3f) | 0x80);
michael@0 1222 return 3;
michael@0 1223 }
michael@0 1224 if (c < 0x110000) {
michael@0 1225 buf[0] = (char)((c >> 18) | UTF8_cval4);
michael@0 1226 buf[1] = (char)(((c >> 12) & 0x3f) | 0x80);
michael@0 1227 buf[2] = (char)(((c >> 6) & 0x3f) | 0x80);
michael@0 1228 buf[3] = (char)((c & 0x3f) | 0x80);
michael@0 1229 return 4;
michael@0 1230 }
michael@0 1231 return 0;
michael@0 1232 }
michael@0 1233
michael@0 1234 int FASTCALL
michael@0 1235 XmlUtf16Encode(int charNum, unsigned short *buf)
michael@0 1236 {
michael@0 1237 if (charNum < 0)
michael@0 1238 return 0;
michael@0 1239 if (charNum < 0x10000) {
michael@0 1240 buf[0] = (unsigned short)charNum;
michael@0 1241 return 1;
michael@0 1242 }
michael@0 1243 if (charNum < 0x110000) {
michael@0 1244 charNum -= 0x10000;
michael@0 1245 buf[0] = (unsigned short)((charNum >> 10) + 0xD800);
michael@0 1246 buf[1] = (unsigned short)((charNum & 0x3FF) + 0xDC00);
michael@0 1247 return 2;
michael@0 1248 }
michael@0 1249 return 0;
michael@0 1250 }
michael@0 1251
michael@0 1252 struct unknown_encoding {
michael@0 1253 struct normal_encoding normal;
michael@0 1254 CONVERTER convert;
michael@0 1255 void *userData;
michael@0 1256 unsigned short utf16[256];
michael@0 1257 char utf8[256][4];
michael@0 1258 };
michael@0 1259
michael@0 1260 #define AS_UNKNOWN_ENCODING(enc) ((const struct unknown_encoding *) (enc))
michael@0 1261
michael@0 1262 int
michael@0 1263 XmlSizeOfUnknownEncoding(void)
michael@0 1264 {
michael@0 1265 return sizeof(struct unknown_encoding);
michael@0 1266 }
michael@0 1267
michael@0 1268 static int PTRFASTCALL
michael@0 1269 unknown_isName(const ENCODING *enc, const char *p)
michael@0 1270 {
michael@0 1271 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
michael@0 1272 int c = uenc->convert(uenc->userData, p);
michael@0 1273 if (c & ~0xFFFF)
michael@0 1274 return 0;
michael@0 1275 return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
michael@0 1276 }
michael@0 1277
michael@0 1278 static int PTRFASTCALL
michael@0 1279 unknown_isNmstrt(const ENCODING *enc, const char *p)
michael@0 1280 {
michael@0 1281 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
michael@0 1282 int c = uenc->convert(uenc->userData, p);
michael@0 1283 if (c & ~0xFFFF)
michael@0 1284 return 0;
michael@0 1285 return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
michael@0 1286 }
michael@0 1287
michael@0 1288 static int PTRFASTCALL
michael@0 1289 unknown_isInvalid(const ENCODING *enc, const char *p)
michael@0 1290 {
michael@0 1291 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
michael@0 1292 int c = uenc->convert(uenc->userData, p);
michael@0 1293 return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
michael@0 1294 }
michael@0 1295
michael@0 1296 static void PTRCALL
michael@0 1297 unknown_toUtf8(const ENCODING *enc,
michael@0 1298 const char **fromP, const char *fromLim,
michael@0 1299 char **toP, const char *toLim)
michael@0 1300 {
michael@0 1301 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
michael@0 1302 char buf[XML_UTF8_ENCODE_MAX];
michael@0 1303 for (;;) {
michael@0 1304 const char *utf8;
michael@0 1305 int n;
michael@0 1306 if (*fromP == fromLim)
michael@0 1307 break;
michael@0 1308 utf8 = uenc->utf8[(unsigned char)**fromP];
michael@0 1309 n = *utf8++;
michael@0 1310 if (n == 0) {
michael@0 1311 int c = uenc->convert(uenc->userData, *fromP);
michael@0 1312 n = XmlUtf8Encode(c, buf);
michael@0 1313 if (n > toLim - *toP)
michael@0 1314 break;
michael@0 1315 utf8 = buf;
michael@0 1316 *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
michael@0 1317 - (BT_LEAD2 - 2));
michael@0 1318 }
michael@0 1319 else {
michael@0 1320 if (n > toLim - *toP)
michael@0 1321 break;
michael@0 1322 (*fromP)++;
michael@0 1323 }
michael@0 1324 do {
michael@0 1325 *(*toP)++ = *utf8++;
michael@0 1326 } while (--n != 0);
michael@0 1327 }
michael@0 1328 }
michael@0 1329
michael@0 1330 static void PTRCALL
michael@0 1331 unknown_toUtf16(const ENCODING *enc,
michael@0 1332 const char **fromP, const char *fromLim,
michael@0 1333 unsigned short **toP, const unsigned short *toLim)
michael@0 1334 {
michael@0 1335 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
michael@0 1336 while (*fromP != fromLim && *toP != toLim) {
michael@0 1337 unsigned short c = uenc->utf16[(unsigned char)**fromP];
michael@0 1338 if (c == 0) {
michael@0 1339 c = (unsigned short)
michael@0 1340 uenc->convert(uenc->userData, *fromP);
michael@0 1341 *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
michael@0 1342 - (BT_LEAD2 - 2));
michael@0 1343 }
michael@0 1344 else
michael@0 1345 (*fromP)++;
michael@0 1346 *(*toP)++ = c;
michael@0 1347 }
michael@0 1348 }
michael@0 1349
michael@0 1350 ENCODING *
michael@0 1351 XmlInitUnknownEncoding(void *mem,
michael@0 1352 int *table,
michael@0 1353 CONVERTER convert,
michael@0 1354 void *userData)
michael@0 1355 {
michael@0 1356 int i;
michael@0 1357 struct unknown_encoding *e = (struct unknown_encoding *)mem;
michael@0 1358 for (i = 0; i < (int)sizeof(struct normal_encoding); i++)
michael@0 1359 ((char *)mem)[i] = ((char *)&latin1_encoding)[i];
michael@0 1360 for (i = 0; i < 128; i++)
michael@0 1361 if (latin1_encoding.type[i] != BT_OTHER
michael@0 1362 && latin1_encoding.type[i] != BT_NONXML
michael@0 1363 && table[i] != i)
michael@0 1364 return 0;
michael@0 1365 for (i = 0; i < 256; i++) {
michael@0 1366 int c = table[i];
michael@0 1367 if (c == -1) {
michael@0 1368 e->normal.type[i] = BT_MALFORM;
michael@0 1369 /* This shouldn't really get used. */
michael@0 1370 e->utf16[i] = 0xFFFF;
michael@0 1371 e->utf8[i][0] = 1;
michael@0 1372 e->utf8[i][1] = 0;
michael@0 1373 }
michael@0 1374 else if (c < 0) {
michael@0 1375 if (c < -4)
michael@0 1376 return 0;
michael@0 1377 e->normal.type[i] = (unsigned char)(BT_LEAD2 - (c + 2));
michael@0 1378 e->utf8[i][0] = 0;
michael@0 1379 e->utf16[i] = 0;
michael@0 1380 }
michael@0 1381 else if (c < 0x80) {
michael@0 1382 if (latin1_encoding.type[c] != BT_OTHER
michael@0 1383 && latin1_encoding.type[c] != BT_NONXML
michael@0 1384 && c != i)
michael@0 1385 return 0;
michael@0 1386 e->normal.type[i] = latin1_encoding.type[c];
michael@0 1387 e->utf8[i][0] = 1;
michael@0 1388 e->utf8[i][1] = (char)c;
michael@0 1389 e->utf16[i] = (unsigned short)(c == 0 ? 0xFFFF : c);
michael@0 1390 }
michael@0 1391 else if (checkCharRefNumber(c) < 0) {
michael@0 1392 e->normal.type[i] = BT_NONXML;
michael@0 1393 /* This shouldn't really get used. */
michael@0 1394 e->utf16[i] = 0xFFFF;
michael@0 1395 e->utf8[i][0] = 1;
michael@0 1396 e->utf8[i][1] = 0;
michael@0 1397 }
michael@0 1398 else {
michael@0 1399 if (c > 0xFFFF)
michael@0 1400 return 0;
michael@0 1401 if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
michael@0 1402 e->normal.type[i] = BT_NMSTRT;
michael@0 1403 else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
michael@0 1404 e->normal.type[i] = BT_NAME;
michael@0 1405 else
michael@0 1406 e->normal.type[i] = BT_OTHER;
michael@0 1407 e->utf8[i][0] = (char)XmlUtf8Encode(c, e->utf8[i] + 1);
michael@0 1408 e->utf16[i] = (unsigned short)c;
michael@0 1409 }
michael@0 1410 }
michael@0 1411 e->userData = userData;
michael@0 1412 e->convert = convert;
michael@0 1413 if (convert) {
michael@0 1414 e->normal.isName2 = unknown_isName;
michael@0 1415 e->normal.isName3 = unknown_isName;
michael@0 1416 e->normal.isName4 = unknown_isName;
michael@0 1417 e->normal.isNmstrt2 = unknown_isNmstrt;
michael@0 1418 e->normal.isNmstrt3 = unknown_isNmstrt;
michael@0 1419 e->normal.isNmstrt4 = unknown_isNmstrt;
michael@0 1420 e->normal.isInvalid2 = unknown_isInvalid;
michael@0 1421 e->normal.isInvalid3 = unknown_isInvalid;
michael@0 1422 e->normal.isInvalid4 = unknown_isInvalid;
michael@0 1423 }
michael@0 1424 e->normal.enc.utf8Convert = unknown_toUtf8;
michael@0 1425 e->normal.enc.utf16Convert = unknown_toUtf16;
michael@0 1426 return &(e->normal.enc);
michael@0 1427 }
michael@0 1428
michael@0 1429 /* If this enumeration is changed, getEncodingIndex and encodings
michael@0 1430 must also be changed. */
michael@0 1431 enum {
michael@0 1432 UNKNOWN_ENC = -1,
michael@0 1433 ISO_8859_1_ENC = 0,
michael@0 1434 US_ASCII_ENC,
michael@0 1435 UTF_8_ENC,
michael@0 1436 UTF_16_ENC,
michael@0 1437 UTF_16BE_ENC,
michael@0 1438 UTF_16LE_ENC,
michael@0 1439 /* must match encodingNames up to here */
michael@0 1440 NO_ENC
michael@0 1441 };
michael@0 1442
michael@0 1443 static const char KW_ISO_8859_1[] = {
michael@0 1444 ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8, ASCII_5, ASCII_9,
michael@0 1445 ASCII_MINUS, ASCII_1, '\0'
michael@0 1446 };
michael@0 1447 static const char KW_US_ASCII[] = {
michael@0 1448 ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S, ASCII_C, ASCII_I, ASCII_I,
michael@0 1449 '\0'
michael@0 1450 };
michael@0 1451 static const char KW_UTF_8[] = {
michael@0 1452 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
michael@0 1453 };
michael@0 1454 static const char KW_UTF_16[] = {
michael@0 1455 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
michael@0 1456 };
michael@0 1457 static const char KW_UTF_16BE[] = {
michael@0 1458 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_B, ASCII_E,
michael@0 1459 '\0'
michael@0 1460 };
michael@0 1461 static const char KW_UTF_16LE[] = {
michael@0 1462 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_L, ASCII_E,
michael@0 1463 '\0'
michael@0 1464 };
michael@0 1465
michael@0 1466 static int FASTCALL
michael@0 1467 getEncodingIndex(const char *name)
michael@0 1468 {
michael@0 1469 static const char * const encodingNames[] = {
michael@0 1470 KW_ISO_8859_1,
michael@0 1471 KW_US_ASCII,
michael@0 1472 KW_UTF_8,
michael@0 1473 KW_UTF_16,
michael@0 1474 KW_UTF_16BE,
michael@0 1475 KW_UTF_16LE,
michael@0 1476 };
michael@0 1477 int i;
michael@0 1478 if (name == NULL)
michael@0 1479 return NO_ENC;
michael@0 1480 for (i = 0; i < (int)(sizeof(encodingNames)/sizeof(encodingNames[0])); i++)
michael@0 1481 if (streqci(name, encodingNames[i]))
michael@0 1482 return i;
michael@0 1483 return UNKNOWN_ENC;
michael@0 1484 }
michael@0 1485
michael@0 1486 /* For binary compatibility, we store the index of the encoding
michael@0 1487 specified at initialization in the isUtf16 member.
michael@0 1488 */
michael@0 1489
michael@0 1490 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
michael@0 1491 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
michael@0 1492
michael@0 1493 /* This is what detects the encoding. encodingTable maps from
michael@0 1494 encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
michael@0 1495 the external (protocol) specified encoding; state is
michael@0 1496 XML_CONTENT_STATE if we're parsing an external text entity, and
michael@0 1497 XML_PROLOG_STATE otherwise.
michael@0 1498 */
michael@0 1499
michael@0 1500
michael@0 1501 static int
michael@0 1502 initScan(const ENCODING * const *encodingTable,
michael@0 1503 const INIT_ENCODING *enc,
michael@0 1504 int state,
michael@0 1505 const char *ptr,
michael@0 1506 const char *end,
michael@0 1507 const char **nextTokPtr)
michael@0 1508 {
michael@0 1509 const ENCODING **encPtr;
michael@0 1510
michael@0 1511 if (ptr == end)
michael@0 1512 return XML_TOK_NONE;
michael@0 1513 encPtr = enc->encPtr;
michael@0 1514 if (ptr + 1 == end) {
michael@0 1515 /* only a single byte available for auto-detection */
michael@0 1516 #ifndef XML_DTD /* FIXME */
michael@0 1517 /* a well-formed document entity must have more than one byte */
michael@0 1518 if (state != XML_CONTENT_STATE)
michael@0 1519 return XML_TOK_PARTIAL;
michael@0 1520 #endif
michael@0 1521 /* so we're parsing an external text entity... */
michael@0 1522 /* if UTF-16 was externally specified, then we need at least 2 bytes */
michael@0 1523 switch (INIT_ENC_INDEX(enc)) {
michael@0 1524 case UTF_16_ENC:
michael@0 1525 case UTF_16LE_ENC:
michael@0 1526 case UTF_16BE_ENC:
michael@0 1527 return XML_TOK_PARTIAL;
michael@0 1528 }
michael@0 1529 switch ((unsigned char)*ptr) {
michael@0 1530 case 0xFE:
michael@0 1531 case 0xFF:
michael@0 1532 case 0xEF: /* possibly first byte of UTF-8 BOM */
michael@0 1533 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
michael@0 1534 && state == XML_CONTENT_STATE)
michael@0 1535 break;
michael@0 1536 /* fall through */
michael@0 1537 case 0x00:
michael@0 1538 case 0x3C:
michael@0 1539 return XML_TOK_PARTIAL;
michael@0 1540 }
michael@0 1541 }
michael@0 1542 else {
michael@0 1543 switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
michael@0 1544 case 0xFEFF:
michael@0 1545 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
michael@0 1546 && state == XML_CONTENT_STATE)
michael@0 1547 break;
michael@0 1548 *nextTokPtr = ptr + 2;
michael@0 1549 *encPtr = encodingTable[UTF_16BE_ENC];
michael@0 1550 return XML_TOK_BOM;
michael@0 1551 /* 00 3C is handled in the default case */
michael@0 1552 case 0x3C00:
michael@0 1553 if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
michael@0 1554 || INIT_ENC_INDEX(enc) == UTF_16_ENC)
michael@0 1555 && state == XML_CONTENT_STATE)
michael@0 1556 break;
michael@0 1557 *encPtr = encodingTable[UTF_16LE_ENC];
michael@0 1558 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
michael@0 1559 case 0xFFFE:
michael@0 1560 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
michael@0 1561 && state == XML_CONTENT_STATE)
michael@0 1562 break;
michael@0 1563 *nextTokPtr = ptr + 2;
michael@0 1564 *encPtr = encodingTable[UTF_16LE_ENC];
michael@0 1565 return XML_TOK_BOM;
michael@0 1566 case 0xEFBB:
michael@0 1567 /* Maybe a UTF-8 BOM (EF BB BF) */
michael@0 1568 /* If there's an explicitly specified (external) encoding
michael@0 1569 of ISO-8859-1 or some flavour of UTF-16
michael@0 1570 and this is an external text entity,
michael@0 1571 don't look for the BOM,
michael@0 1572 because it might be a legal data.
michael@0 1573 */
michael@0 1574 if (state == XML_CONTENT_STATE) {
michael@0 1575 int e = INIT_ENC_INDEX(enc);
michael@0 1576 if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC
michael@0 1577 || e == UTF_16LE_ENC || e == UTF_16_ENC)
michael@0 1578 break;
michael@0 1579 }
michael@0 1580 if (ptr + 2 == end)
michael@0 1581 return XML_TOK_PARTIAL;
michael@0 1582 if ((unsigned char)ptr[2] == 0xBF) {
michael@0 1583 *nextTokPtr = ptr + 3;
michael@0 1584 *encPtr = encodingTable[UTF_8_ENC];
michael@0 1585 return XML_TOK_BOM;
michael@0 1586 }
michael@0 1587 break;
michael@0 1588 default:
michael@0 1589 if (ptr[0] == '\0') {
michael@0 1590 /* 0 isn't a legal data character. Furthermore a document
michael@0 1591 entity can only start with ASCII characters. So the only
michael@0 1592 way this can fail to be big-endian UTF-16 if it it's an
michael@0 1593 external parsed general entity that's labelled as
michael@0 1594 UTF-16LE.
michael@0 1595 */
michael@0 1596 if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
michael@0 1597 break;
michael@0 1598 *encPtr = encodingTable[UTF_16BE_ENC];
michael@0 1599 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
michael@0 1600 }
michael@0 1601 else if (ptr[1] == '\0') {
michael@0 1602 /* We could recover here in the case:
michael@0 1603 - parsing an external entity
michael@0 1604 - second byte is 0
michael@0 1605 - no externally specified encoding
michael@0 1606 - no encoding declaration
michael@0 1607 by assuming UTF-16LE. But we don't, because this would mean when
michael@0 1608 presented just with a single byte, we couldn't reliably determine
michael@0 1609 whether we needed further bytes.
michael@0 1610 */
michael@0 1611 if (state == XML_CONTENT_STATE)
michael@0 1612 break;
michael@0 1613 *encPtr = encodingTable[UTF_16LE_ENC];
michael@0 1614 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
michael@0 1615 }
michael@0 1616 break;
michael@0 1617 }
michael@0 1618 }
michael@0 1619 *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
michael@0 1620 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
michael@0 1621 }
michael@0 1622
michael@0 1623
michael@0 1624 #define NS(x) x
michael@0 1625 #define ns(x) x
michael@0 1626 #include "xmltok_ns.c"
michael@0 1627 #undef NS
michael@0 1628 #undef ns
michael@0 1629
michael@0 1630 #ifdef XML_NS
michael@0 1631
michael@0 1632 #define NS(x) x ## NS
michael@0 1633 #define ns(x) x ## _ns
michael@0 1634
michael@0 1635 #include "xmltok_ns.c"
michael@0 1636
michael@0 1637 #undef NS
michael@0 1638 #undef ns
michael@0 1639
michael@0 1640 ENCODING *
michael@0 1641 XmlInitUnknownEncodingNS(void *mem,
michael@0 1642 int *table,
michael@0 1643 CONVERTER convert,
michael@0 1644 void *userData)
michael@0 1645 {
michael@0 1646 ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
michael@0 1647 if (enc)
michael@0 1648 ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;
michael@0 1649 return enc;
michael@0 1650 }
michael@0 1651
michael@0 1652 #endif /* XML_NS */
michael@0 1653
michael@0 1654 /* BEGIN MOZILLA CHANGE (Mozilla extensions for QName checking) */
michael@0 1655 #ifdef MOZILLA_CLIENT
michael@0 1656 #include "moz_extensions.c"
michael@0 1657 #endif /* MOZILLA_CLIENT */
michael@0 1658 /* END MOZILLA CHANGE */

mercurial