alsa.audio: limit the supported frequencies to common set
[AROS.git] / workbench / libs / expat / lib / xmltok.c
blobac8433dbd281ddf961f2554c8bc13837dbc47332
1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
3 */
5 #include <stddef.h>
7 #ifdef COMPILED_FROM_DSP
8 #include "winconfig.h"
9 #elif defined(MACOS_CLASSIC)
10 #include "macconfig.h"
11 #elif defined(__amigaos__)
12 #include "amigaconfig.h"
13 #elif defined(__AROS__)
14 #include "arosconfig.h"
15 #elif defined(__WATCOMC__)
16 #include "watcomconfig.h"
17 #else
18 #ifdef HAVE_EXPAT_CONFIG_H
19 #include <expat_config.h>
20 #endif
21 #endif /* ndef COMPILED_FROM_DSP */
23 #include "expat_external.h"
24 #include "internal.h"
25 #include "xmltok.h"
26 #include "nametab.h"
28 #ifdef XML_DTD
29 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
30 #else
31 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
32 #endif
34 #define VTABLE1 \
35 { PREFIX(prologTok), PREFIX(contentTok), \
36 PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
37 { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
38 PREFIX(sameName), \
39 PREFIX(nameMatchesAscii), \
40 PREFIX(nameLength), \
41 PREFIX(skipS), \
42 PREFIX(getAtts), \
43 PREFIX(charRefNumber), \
44 PREFIX(predefinedEntityName), \
45 PREFIX(updatePosition), \
46 PREFIX(isPublicId)
48 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
50 #define UCS2_GET_NAMING(pages, hi, lo) \
51 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
53 /* A 2 byte UTF-8 representation splits the characters 11 bits between
54 the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
55 pages, 3 bits to add to that index and 5 bits to generate the mask.
57 #define UTF8_GET_NAMING2(pages, byte) \
58 (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
59 + ((((byte)[0]) & 3) << 1) \
60 + ((((byte)[1]) >> 5) & 1)] \
61 & (1 << (((byte)[1]) & 0x1F)))
63 /* A 3 byte UTF-8 representation splits the characters 16 bits between
64 the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
65 into pages, 3 bits to add to that index and 5 bits to generate the
66 mask.
68 #define UTF8_GET_NAMING3(pages, byte) \
69 (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
70 + ((((byte)[1]) >> 2) & 0xF)] \
71 << 3) \
72 + ((((byte)[1]) & 3) << 1) \
73 + ((((byte)[2]) >> 5) & 1)] \
74 & (1 << (((byte)[2]) & 0x1F)))
76 #define UTF8_GET_NAMING(pages, p, n) \
77 ((n) == 2 \
78 ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
79 : ((n) == 3 \
80 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
81 : 0))
83 /* Detection of invalid UTF-8 sequences is based on Table 3.1B
84 of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
85 with the additional restriction of not allowing the Unicode
86 code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
87 Implementation details:
88 (A & 0x80) == 0 means A < 0x80
89 and
90 (A & 0xC0) == 0xC0 means A > 0xBF
93 #define UTF8_INVALID2(p) \
94 ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
96 #define UTF8_INVALID3(p) \
97 (((p)[2] & 0x80) == 0 \
98 || \
99 ((*p) == 0xEF && (p)[1] == 0xBF \
101 (p)[2] > 0xBD \
103 ((p)[2] & 0xC0) == 0xC0) \
104 || \
105 ((*p) == 0xE0 \
107 (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
109 ((p)[1] & 0x80) == 0 \
110 || \
111 ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
113 #define UTF8_INVALID4(p) \
114 (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 \
115 || \
116 ((p)[2] & 0x80) == 0 || ((p)[2] & 0xC0) == 0xC0 \
117 || \
118 ((*p) == 0xF0 \
120 (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
122 ((p)[1] & 0x80) == 0 \
123 || \
124 ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
126 static int PTRFASTCALL
127 isNever(const ENCODING *enc, const char *p)
129 return 0;
132 static int PTRFASTCALL
133 utf8_isName2(const ENCODING *enc, const char *p)
135 return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
138 static int PTRFASTCALL
139 utf8_isName3(const ENCODING *enc, const char *p)
141 return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
144 #define utf8_isName4 isNever
146 static int PTRFASTCALL
147 utf8_isNmstrt2(const ENCODING *enc, const char *p)
149 return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
152 static int PTRFASTCALL
153 utf8_isNmstrt3(const ENCODING *enc, const char *p)
155 return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
158 #define utf8_isNmstrt4 isNever
160 static int PTRFASTCALL
161 utf8_isInvalid2(const ENCODING *enc, const char *p)
163 return UTF8_INVALID2((const unsigned char *)p);
166 static int PTRFASTCALL
167 utf8_isInvalid3(const ENCODING *enc, const char *p)
169 return UTF8_INVALID3((const unsigned char *)p);
172 static int PTRFASTCALL
173 utf8_isInvalid4(const ENCODING *enc, const char *p)
175 return UTF8_INVALID4((const unsigned char *)p);
178 struct normal_encoding {
179 ENCODING enc;
180 unsigned char type[256];
181 #ifdef XML_MIN_SIZE
182 int (PTRFASTCALL *byteType)(const ENCODING *, const char *);
183 int (PTRFASTCALL *isNameMin)(const ENCODING *, const char *);
184 int (PTRFASTCALL *isNmstrtMin)(const ENCODING *, const char *);
185 int (PTRFASTCALL *byteToAscii)(const ENCODING *, const char *);
186 int (PTRCALL *charMatches)(const ENCODING *, const char *, int);
187 #endif /* XML_MIN_SIZE */
188 int (PTRFASTCALL *isName2)(const ENCODING *, const char *);
189 int (PTRFASTCALL *isName3)(const ENCODING *, const char *);
190 int (PTRFASTCALL *isName4)(const ENCODING *, const char *);
191 int (PTRFASTCALL *isNmstrt2)(const ENCODING *, const char *);
192 int (PTRFASTCALL *isNmstrt3)(const ENCODING *, const char *);
193 int (PTRFASTCALL *isNmstrt4)(const ENCODING *, const char *);
194 int (PTRFASTCALL *isInvalid2)(const ENCODING *, const char *);
195 int (PTRFASTCALL *isInvalid3)(const ENCODING *, const char *);
196 int (PTRFASTCALL *isInvalid4)(const ENCODING *, const char *);
199 #define AS_NORMAL_ENCODING(enc) ((const struct normal_encoding *) (enc))
201 #ifdef XML_MIN_SIZE
203 #define STANDARD_VTABLE(E) \
204 E ## byteType, \
205 E ## isNameMin, \
206 E ## isNmstrtMin, \
207 E ## byteToAscii, \
208 E ## charMatches,
210 #else
212 #define STANDARD_VTABLE(E) /* as nothing */
214 #endif
216 #define NORMAL_VTABLE(E) \
217 E ## isName2, \
218 E ## isName3, \
219 E ## isName4, \
220 E ## isNmstrt2, \
221 E ## isNmstrt3, \
222 E ## isNmstrt4, \
223 E ## isInvalid2, \
224 E ## isInvalid3, \
225 E ## isInvalid4
227 static int FASTCALL checkCharRefNumber(int);
229 #include "xmltok_impl.h"
230 #include "ascii.h"
232 #ifdef XML_MIN_SIZE
233 #define sb_isNameMin isNever
234 #define sb_isNmstrtMin isNever
235 #endif
237 #ifdef XML_MIN_SIZE
238 #define MINBPC(enc) ((enc)->minBytesPerChar)
239 #else
240 /* minimum bytes per character */
241 #define MINBPC(enc) 1
242 #endif
244 #define SB_BYTE_TYPE(enc, p) \
245 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
247 #ifdef XML_MIN_SIZE
248 static int PTRFASTCALL
249 sb_byteType(const ENCODING *enc, const char *p)
251 return SB_BYTE_TYPE(enc, p);
253 #define BYTE_TYPE(enc, p) \
254 (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
255 #else
256 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
257 #endif
259 #ifdef XML_MIN_SIZE
260 #define BYTE_TO_ASCII(enc, p) \
261 (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
262 static int PTRFASTCALL
263 sb_byteToAscii(const ENCODING *enc, const char *p)
265 return *p;
267 #else
268 #define BYTE_TO_ASCII(enc, p) (*(p))
269 #endif
271 #define IS_NAME_CHAR(enc, p, n) \
272 (AS_NORMAL_ENCODING(enc)->isName ## n(enc, p))
273 #define IS_NMSTRT_CHAR(enc, p, n) \
274 (AS_NORMAL_ENCODING(enc)->isNmstrt ## n(enc, p))
275 #define IS_INVALID_CHAR(enc, p, n) \
276 (AS_NORMAL_ENCODING(enc)->isInvalid ## n(enc, p))
278 #ifdef XML_MIN_SIZE
279 #define IS_NAME_CHAR_MINBPC(enc, p) \
280 (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
281 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
282 (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
283 #else
284 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
285 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
286 #endif
288 #ifdef XML_MIN_SIZE
289 #define CHAR_MATCHES(enc, p, c) \
290 (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
291 static int PTRCALL
292 sb_charMatches(const ENCODING *enc, const char *p, int c)
294 return *p == c;
296 #else
297 /* c is an ASCII character */
298 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
299 #endif
301 #define PREFIX(ident) normal_ ## ident
302 #define XML_TOK_IMPL_C
303 #include "xmltok_impl.c"
304 #undef XML_TOK_IMPL_C
306 #undef MINBPC
307 #undef BYTE_TYPE
308 #undef BYTE_TO_ASCII
309 #undef CHAR_MATCHES
310 #undef IS_NAME_CHAR
311 #undef IS_NAME_CHAR_MINBPC
312 #undef IS_NMSTRT_CHAR
313 #undef IS_NMSTRT_CHAR_MINBPC
314 #undef IS_INVALID_CHAR
316 enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
317 UTF8_cval1 = 0x00,
318 UTF8_cval2 = 0xc0,
319 UTF8_cval3 = 0xe0,
320 UTF8_cval4 = 0xf0
323 static void PTRCALL
324 utf8_toUtf8(const ENCODING *enc,
325 const char **fromP, const char *fromLim,
326 char **toP, const char *toLim)
328 char *to;
329 const char *from;
330 if (fromLim - *fromP > toLim - *toP) {
331 /* Avoid copying partial characters. */
332 for (fromLim = *fromP + (toLim - *toP); fromLim > *fromP; fromLim--)
333 if (((unsigned char)fromLim[-1] & 0xc0) != 0x80)
334 break;
336 for (to = *toP, from = *fromP; from != fromLim; from++, to++)
337 *to = *from;
338 *fromP = from;
339 *toP = to;
342 static void PTRCALL
343 utf8_toUtf16(const ENCODING *enc,
344 const char **fromP, const char *fromLim,
345 unsigned short **toP, const unsigned short *toLim)
347 unsigned short *to = *toP;
348 const char *from = *fromP;
349 while (from != fromLim && to != toLim) {
350 switch (((struct normal_encoding *)enc)->type[(unsigned char)*from]) {
351 case BT_LEAD2:
352 *to++ = (unsigned short)(((from[0] & 0x1f) << 6) | (from[1] & 0x3f));
353 from += 2;
354 break;
355 case BT_LEAD3:
356 *to++ = (unsigned short)(((from[0] & 0xf) << 12)
357 | ((from[1] & 0x3f) << 6) | (from[2] & 0x3f));
358 from += 3;
359 break;
360 case BT_LEAD4:
362 unsigned long n;
363 if (to + 1 == toLim)
364 goto after;
365 n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12)
366 | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
367 n -= 0x10000;
368 to[0] = (unsigned short)((n >> 10) | 0xD800);
369 to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
370 to += 2;
371 from += 4;
373 break;
374 default:
375 *to++ = *from++;
376 break;
379 after:
380 *fromP = from;
381 *toP = to;
384 #ifdef XML_NS
385 static const struct normal_encoding utf8_encoding_ns = {
386 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
388 #include "asciitab.h"
389 #include "utf8tab.h"
391 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
393 #endif
395 static const struct normal_encoding utf8_encoding = {
396 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
398 #define BT_COLON BT_NMSTRT
399 #include "asciitab.h"
400 #undef BT_COLON
401 #include "utf8tab.h"
403 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
406 #ifdef XML_NS
408 static const struct normal_encoding internal_utf8_encoding_ns = {
409 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
411 #include "iasciitab.h"
412 #include "utf8tab.h"
414 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
417 #endif
419 static const struct normal_encoding internal_utf8_encoding = {
420 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
422 #define BT_COLON BT_NMSTRT
423 #include "iasciitab.h"
424 #undef BT_COLON
425 #include "utf8tab.h"
427 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
430 static void PTRCALL
431 latin1_toUtf8(const ENCODING *enc,
432 const char **fromP, const char *fromLim,
433 char **toP, const char *toLim)
435 for (;;) {
436 unsigned char c;
437 if (*fromP == fromLim)
438 break;
439 c = (unsigned char)**fromP;
440 if (c & 0x80) {
441 if (toLim - *toP < 2)
442 break;
443 *(*toP)++ = (char)((c >> 6) | UTF8_cval2);
444 *(*toP)++ = (char)((c & 0x3f) | 0x80);
445 (*fromP)++;
447 else {
448 if (*toP == toLim)
449 break;
450 *(*toP)++ = *(*fromP)++;
455 static void PTRCALL
456 latin1_toUtf16(const ENCODING *enc,
457 const char **fromP, const char *fromLim,
458 unsigned short **toP, const unsigned short *toLim)
460 while (*fromP != fromLim && *toP != toLim)
461 *(*toP)++ = (unsigned char)*(*fromP)++;
464 #ifdef XML_NS
466 static const struct normal_encoding latin1_encoding_ns = {
467 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
469 #include "asciitab.h"
470 #include "latin1tab.h"
472 STANDARD_VTABLE(sb_)
475 #endif
477 static const struct normal_encoding latin1_encoding = {
478 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
480 #define BT_COLON BT_NMSTRT
481 #include "asciitab.h"
482 #undef BT_COLON
483 #include "latin1tab.h"
485 STANDARD_VTABLE(sb_)
488 static void PTRCALL
489 ascii_toUtf8(const ENCODING *enc,
490 const char **fromP, const char *fromLim,
491 char **toP, const char *toLim)
493 while (*fromP != fromLim && *toP != toLim)
494 *(*toP)++ = *(*fromP)++;
497 #ifdef XML_NS
499 static const struct normal_encoding ascii_encoding_ns = {
500 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
502 #include "asciitab.h"
503 /* BT_NONXML == 0 */
505 STANDARD_VTABLE(sb_)
508 #endif
510 static const struct normal_encoding ascii_encoding = {
511 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
513 #define BT_COLON BT_NMSTRT
514 #include "asciitab.h"
515 #undef BT_COLON
516 /* BT_NONXML == 0 */
518 STANDARD_VTABLE(sb_)
521 static int PTRFASTCALL
522 unicode_byte_type(char hi, char lo)
524 switch ((unsigned char)hi) {
525 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
526 return BT_LEAD4;
527 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
528 return BT_TRAIL;
529 case 0xFF:
530 switch ((unsigned char)lo) {
531 case 0xFF:
532 case 0xFE:
533 return BT_NONXML;
535 break;
537 return BT_NONASCII;
540 #define DEFINE_UTF16_TO_UTF8(E) \
541 static void PTRCALL \
542 E ## toUtf8(const ENCODING *enc, \
543 const char **fromP, const char *fromLim, \
544 char **toP, const char *toLim) \
546 const char *from; \
547 for (from = *fromP; from != fromLim; from += 2) { \
548 int plane; \
549 unsigned char lo2; \
550 unsigned char lo = GET_LO(from); \
551 unsigned char hi = GET_HI(from); \
552 switch (hi) { \
553 case 0: \
554 if (lo < 0x80) { \
555 if (*toP == toLim) { \
556 *fromP = from; \
557 return; \
559 *(*toP)++ = lo; \
560 break; \
562 /* fall through */ \
563 case 0x1: case 0x2: case 0x3: \
564 case 0x4: case 0x5: case 0x6: case 0x7: \
565 if (toLim - *toP < 2) { \
566 *fromP = from; \
567 return; \
569 *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
570 *(*toP)++ = ((lo & 0x3f) | 0x80); \
571 break; \
572 default: \
573 if (toLim - *toP < 3) { \
574 *fromP = from; \
575 return; \
577 /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
578 *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
579 *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
580 *(*toP)++ = ((lo & 0x3f) | 0x80); \
581 break; \
582 case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
583 if (toLim - *toP < 4) { \
584 *fromP = from; \
585 return; \
587 plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
588 *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
589 *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
590 from += 2; \
591 lo2 = GET_LO(from); \
592 *(*toP)++ = (((lo & 0x3) << 4) \
593 | ((GET_HI(from) & 0x3) << 2) \
594 | (lo2 >> 6) \
595 | 0x80); \
596 *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
597 break; \
600 *fromP = from; \
603 #define DEFINE_UTF16_TO_UTF16(E) \
604 static void PTRCALL \
605 E ## toUtf16(const ENCODING *enc, \
606 const char **fromP, const char *fromLim, \
607 unsigned short **toP, const unsigned short *toLim) \
609 /* Avoid copying first half only of surrogate */ \
610 if (fromLim - *fromP > ((toLim - *toP) << 1) \
611 && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
612 fromLim -= 2; \
613 for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
614 *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
617 #define SET2(ptr, ch) \
618 (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
619 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
620 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
622 DEFINE_UTF16_TO_UTF8(little2_)
623 DEFINE_UTF16_TO_UTF16(little2_)
625 #undef SET2
626 #undef GET_LO
627 #undef GET_HI
629 #define SET2(ptr, ch) \
630 (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
631 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
632 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
634 DEFINE_UTF16_TO_UTF8(big2_)
635 DEFINE_UTF16_TO_UTF16(big2_)
637 #undef SET2
638 #undef GET_LO
639 #undef GET_HI
641 #define LITTLE2_BYTE_TYPE(enc, p) \
642 ((p)[1] == 0 \
643 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
644 : unicode_byte_type((p)[1], (p)[0]))
645 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
646 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
647 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
648 UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
649 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
650 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
652 #ifdef XML_MIN_SIZE
654 static int PTRFASTCALL
655 little2_byteType(const ENCODING *enc, const char *p)
657 return LITTLE2_BYTE_TYPE(enc, p);
660 static int PTRFASTCALL
661 little2_byteToAscii(const ENCODING *enc, const char *p)
663 return LITTLE2_BYTE_TO_ASCII(enc, p);
666 static int PTRCALL
667 little2_charMatches(const ENCODING *enc, const char *p, int c)
669 return LITTLE2_CHAR_MATCHES(enc, p, c);
672 static int PTRFASTCALL
673 little2_isNameMin(const ENCODING *enc, const char *p)
675 return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
678 static int PTRFASTCALL
679 little2_isNmstrtMin(const ENCODING *enc, const char *p)
681 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
684 #undef VTABLE
685 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
687 #else /* not XML_MIN_SIZE */
689 #undef PREFIX
690 #define PREFIX(ident) little2_ ## ident
691 #define MINBPC(enc) 2
692 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
693 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
694 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
695 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
696 #define IS_NAME_CHAR(enc, p, n) 0
697 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
698 #define IS_NMSTRT_CHAR(enc, p, n) (0)
699 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
701 #define XML_TOK_IMPL_C
702 #include "xmltok_impl.c"
703 #undef XML_TOK_IMPL_C
705 #undef MINBPC
706 #undef BYTE_TYPE
707 #undef BYTE_TO_ASCII
708 #undef CHAR_MATCHES
709 #undef IS_NAME_CHAR
710 #undef IS_NAME_CHAR_MINBPC
711 #undef IS_NMSTRT_CHAR
712 #undef IS_NMSTRT_CHAR_MINBPC
713 #undef IS_INVALID_CHAR
715 #endif /* not XML_MIN_SIZE */
717 #ifdef XML_NS
719 static const struct normal_encoding little2_encoding_ns = {
720 { VTABLE, 2, 0,
721 #if BYTEORDER == 1234
723 #else
725 #endif
728 #include "asciitab.h"
729 #include "latin1tab.h"
731 STANDARD_VTABLE(little2_)
734 #endif
736 static const struct normal_encoding little2_encoding = {
737 { VTABLE, 2, 0,
738 #if BYTEORDER == 1234
740 #else
742 #endif
745 #define BT_COLON BT_NMSTRT
746 #include "asciitab.h"
747 #undef BT_COLON
748 #include "latin1tab.h"
750 STANDARD_VTABLE(little2_)
753 #if BYTEORDER != 4321
755 #ifdef XML_NS
757 static const struct normal_encoding internal_little2_encoding_ns = {
758 { VTABLE, 2, 0, 1 },
760 #include "iasciitab.h"
761 #include "latin1tab.h"
763 STANDARD_VTABLE(little2_)
766 #endif
768 static const struct normal_encoding internal_little2_encoding = {
769 { VTABLE, 2, 0, 1 },
771 #define BT_COLON BT_NMSTRT
772 #include "iasciitab.h"
773 #undef BT_COLON
774 #include "latin1tab.h"
776 STANDARD_VTABLE(little2_)
779 #endif
782 #define BIG2_BYTE_TYPE(enc, p) \
783 ((p)[0] == 0 \
784 ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
785 : unicode_byte_type((p)[0], (p)[1]))
786 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
787 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
788 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
789 UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
790 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
791 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
793 #ifdef XML_MIN_SIZE
795 static int PTRFASTCALL
796 big2_byteType(const ENCODING *enc, const char *p)
798 return BIG2_BYTE_TYPE(enc, p);
801 static int PTRFASTCALL
802 big2_byteToAscii(const ENCODING *enc, const char *p)
804 return BIG2_BYTE_TO_ASCII(enc, p);
807 static int PTRCALL
808 big2_charMatches(const ENCODING *enc, const char *p, int c)
810 return BIG2_CHAR_MATCHES(enc, p, c);
813 static int PTRFASTCALL
814 big2_isNameMin(const ENCODING *enc, const char *p)
816 return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
819 static int PTRFASTCALL
820 big2_isNmstrtMin(const ENCODING *enc, const char *p)
822 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
825 #undef VTABLE
826 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
828 #else /* not XML_MIN_SIZE */
830 #undef PREFIX
831 #define PREFIX(ident) big2_ ## ident
832 #define MINBPC(enc) 2
833 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
834 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
835 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
836 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
837 #define IS_NAME_CHAR(enc, p, n) 0
838 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
839 #define IS_NMSTRT_CHAR(enc, p, n) (0)
840 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
842 #define XML_TOK_IMPL_C
843 #include "xmltok_impl.c"
844 #undef XML_TOK_IMPL_C
846 #undef MINBPC
847 #undef BYTE_TYPE
848 #undef BYTE_TO_ASCII
849 #undef CHAR_MATCHES
850 #undef IS_NAME_CHAR
851 #undef IS_NAME_CHAR_MINBPC
852 #undef IS_NMSTRT_CHAR
853 #undef IS_NMSTRT_CHAR_MINBPC
854 #undef IS_INVALID_CHAR
856 #endif /* not XML_MIN_SIZE */
858 #ifdef XML_NS
860 static const struct normal_encoding big2_encoding_ns = {
861 { VTABLE, 2, 0,
862 #if BYTEORDER == 4321
864 #else
866 #endif
869 #include "asciitab.h"
870 #include "latin1tab.h"
872 STANDARD_VTABLE(big2_)
875 #endif
877 static const struct normal_encoding big2_encoding = {
878 { VTABLE, 2, 0,
879 #if BYTEORDER == 4321
881 #else
883 #endif
886 #define BT_COLON BT_NMSTRT
887 #include "asciitab.h"
888 #undef BT_COLON
889 #include "latin1tab.h"
891 STANDARD_VTABLE(big2_)
894 #if BYTEORDER != 1234
896 #ifdef XML_NS
898 static const struct normal_encoding internal_big2_encoding_ns = {
899 { VTABLE, 2, 0, 1 },
901 #include "iasciitab.h"
902 #include "latin1tab.h"
904 STANDARD_VTABLE(big2_)
907 #endif
909 static const struct normal_encoding internal_big2_encoding = {
910 { VTABLE, 2, 0, 1 },
912 #define BT_COLON BT_NMSTRT
913 #include "iasciitab.h"
914 #undef BT_COLON
915 #include "latin1tab.h"
917 STANDARD_VTABLE(big2_)
920 #endif
922 #undef PREFIX
924 static int FASTCALL
925 streqci(const char *s1, const char *s2)
927 for (;;) {
928 char c1 = *s1++;
929 char c2 = *s2++;
930 if (ASCII_a <= c1 && c1 <= ASCII_z)
931 c1 += ASCII_A - ASCII_a;
932 if (ASCII_a <= c2 && c2 <= ASCII_z)
933 c2 += ASCII_A - ASCII_a;
934 if (c1 != c2)
935 return 0;
936 if (!c1)
937 break;
939 return 1;
942 static void PTRCALL
943 initUpdatePosition(const ENCODING *enc, const char *ptr,
944 const char *end, POSITION *pos)
946 normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
949 static int
950 toAscii(const ENCODING *enc, const char *ptr, const char *end)
952 char buf[1];
953 char *p = buf;
954 XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
955 if (p == buf)
956 return -1;
957 else
958 return buf[0];
961 static int FASTCALL
962 isSpace(int c)
964 switch (c) {
965 case 0x20:
966 case 0xD:
967 case 0xA:
968 case 0x9:
969 return 1;
971 return 0;
974 /* Return 1 if there's just optional white space or there's an S
975 followed by name=val.
977 static int
978 parsePseudoAttribute(const ENCODING *enc,
979 const char *ptr,
980 const char *end,
981 const char **namePtr,
982 const char **nameEndPtr,
983 const char **valPtr,
984 const char **nextTokPtr)
986 int c;
987 char open;
988 if (ptr == end) {
989 *namePtr = NULL;
990 return 1;
992 if (!isSpace(toAscii(enc, ptr, end))) {
993 *nextTokPtr = ptr;
994 return 0;
996 do {
997 ptr += enc->minBytesPerChar;
998 } while (isSpace(toAscii(enc, ptr, end)));
999 if (ptr == end) {
1000 *namePtr = NULL;
1001 return 1;
1003 *namePtr = ptr;
1004 for (;;) {
1005 c = toAscii(enc, ptr, end);
1006 if (c == -1) {
1007 *nextTokPtr = ptr;
1008 return 0;
1010 if (c == ASCII_EQUALS) {
1011 *nameEndPtr = ptr;
1012 break;
1014 if (isSpace(c)) {
1015 *nameEndPtr = ptr;
1016 do {
1017 ptr += enc->minBytesPerChar;
1018 } while (isSpace(c = toAscii(enc, ptr, end)));
1019 if (c != ASCII_EQUALS) {
1020 *nextTokPtr = ptr;
1021 return 0;
1023 break;
1025 ptr += enc->minBytesPerChar;
1027 if (ptr == *namePtr) {
1028 *nextTokPtr = ptr;
1029 return 0;
1031 ptr += enc->minBytesPerChar;
1032 c = toAscii(enc, ptr, end);
1033 while (isSpace(c)) {
1034 ptr += enc->minBytesPerChar;
1035 c = toAscii(enc, ptr, end);
1037 if (c != ASCII_QUOT && c != ASCII_APOS) {
1038 *nextTokPtr = ptr;
1039 return 0;
1041 open = (char)c;
1042 ptr += enc->minBytesPerChar;
1043 *valPtr = ptr;
1044 for (;; ptr += enc->minBytesPerChar) {
1045 c = toAscii(enc, ptr, end);
1046 if (c == open)
1047 break;
1048 if (!(ASCII_a <= c && c <= ASCII_z)
1049 && !(ASCII_A <= c && c <= ASCII_Z)
1050 && !(ASCII_0 <= c && c <= ASCII_9)
1051 && c != ASCII_PERIOD
1052 && c != ASCII_MINUS
1053 && c != ASCII_UNDERSCORE) {
1054 *nextTokPtr = ptr;
1055 return 0;
1058 *nextTokPtr = ptr + enc->minBytesPerChar;
1059 return 1;
1062 static const char KW_version[] = {
1063 ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
1066 static const char KW_encoding[] = {
1067 ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
1070 static const char KW_standalone[] = {
1071 ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a, ASCII_l, ASCII_o,
1072 ASCII_n, ASCII_e, '\0'
1075 static const char KW_yes[] = {
1076 ASCII_y, ASCII_e, ASCII_s, '\0'
1079 static const char KW_no[] = {
1080 ASCII_n, ASCII_o, '\0'
1083 static int
1084 doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *,
1085 const char *,
1086 const char *),
1087 int isGeneralTextEntity,
1088 const ENCODING *enc,
1089 const char *ptr,
1090 const char *end,
1091 const char **badPtr,
1092 const char **versionPtr,
1093 const char **versionEndPtr,
1094 const char **encodingName,
1095 const ENCODING **encoding,
1096 int *standalone)
1098 const char *val = NULL;
1099 const char *name = NULL;
1100 const char *nameEnd = NULL;
1101 ptr += 5 * enc->minBytesPerChar;
1102 end -= 2 * enc->minBytesPerChar;
1103 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)
1104 || !name) {
1105 *badPtr = ptr;
1106 return 0;
1108 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1109 if (!isGeneralTextEntity) {
1110 *badPtr = name;
1111 return 0;
1114 else {
1115 if (versionPtr)
1116 *versionPtr = val;
1117 if (versionEndPtr)
1118 *versionEndPtr = ptr;
1119 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1120 *badPtr = ptr;
1121 return 0;
1123 if (!name) {
1124 if (isGeneralTextEntity) {
1125 /* a TextDecl must have an EncodingDecl */
1126 *badPtr = ptr;
1127 return 0;
1129 return 1;
1132 if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
1133 int c = toAscii(enc, val, end);
1134 if (!(ASCII_a <= c && c <= ASCII_z) && !(ASCII_A <= c && c <= ASCII_Z)) {
1135 *badPtr = val;
1136 return 0;
1138 if (encodingName)
1139 *encodingName = val;
1140 if (encoding)
1141 *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
1142 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1143 *badPtr = ptr;
1144 return 0;
1146 if (!name)
1147 return 1;
1149 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone)
1150 || isGeneralTextEntity) {
1151 *badPtr = name;
1152 return 0;
1154 if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1155 if (standalone)
1156 *standalone = 1;
1158 else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1159 if (standalone)
1160 *standalone = 0;
1162 else {
1163 *badPtr = val;
1164 return 0;
1166 while (isSpace(toAscii(enc, ptr, end)))
1167 ptr += enc->minBytesPerChar;
1168 if (ptr != end) {
1169 *badPtr = ptr;
1170 return 0;
1172 return 1;
1175 static int FASTCALL
1176 checkCharRefNumber(int result)
1178 switch (result >> 8) {
1179 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1180 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1181 return -1;
1182 case 0:
1183 if (latin1_encoding.type[result] == BT_NONXML)
1184 return -1;
1185 break;
1186 case 0xFF:
1187 if (result == 0xFFFE || result == 0xFFFF)
1188 return -1;
1189 break;
1191 return result;
1194 int FASTCALL
1195 XmlUtf8Encode(int c, char *buf)
1197 enum {
1198 /* minN is minimum legal resulting value for N byte sequence */
1199 min2 = 0x80,
1200 min3 = 0x800,
1201 min4 = 0x10000
1204 if (c < 0)
1205 return 0;
1206 if (c < min2) {
1207 buf[0] = (char)(c | UTF8_cval1);
1208 return 1;
1210 if (c < min3) {
1211 buf[0] = (char)((c >> 6) | UTF8_cval2);
1212 buf[1] = (char)((c & 0x3f) | 0x80);
1213 return 2;
1215 if (c < min4) {
1216 buf[0] = (char)((c >> 12) | UTF8_cval3);
1217 buf[1] = (char)(((c >> 6) & 0x3f) | 0x80);
1218 buf[2] = (char)((c & 0x3f) | 0x80);
1219 return 3;
1221 if (c < 0x110000) {
1222 buf[0] = (char)((c >> 18) | UTF8_cval4);
1223 buf[1] = (char)(((c >> 12) & 0x3f) | 0x80);
1224 buf[2] = (char)(((c >> 6) & 0x3f) | 0x80);
1225 buf[3] = (char)((c & 0x3f) | 0x80);
1226 return 4;
1228 return 0;
1231 int FASTCALL
1232 XmlUtf16Encode(int charNum, unsigned short *buf)
1234 if (charNum < 0)
1235 return 0;
1236 if (charNum < 0x10000) {
1237 buf[0] = (unsigned short)charNum;
1238 return 1;
1240 if (charNum < 0x110000) {
1241 charNum -= 0x10000;
1242 buf[0] = (unsigned short)((charNum >> 10) + 0xD800);
1243 buf[1] = (unsigned short)((charNum & 0x3FF) + 0xDC00);
1244 return 2;
1246 return 0;
1249 struct unknown_encoding {
1250 struct normal_encoding normal;
1251 CONVERTER convert;
1252 void *userData;
1253 unsigned short utf16[256];
1254 char utf8[256][4];
1257 #define AS_UNKNOWN_ENCODING(enc) ((const struct unknown_encoding *) (enc))
1260 XmlSizeOfUnknownEncoding(void)
1262 return sizeof(struct unknown_encoding);
1265 static int PTRFASTCALL
1266 unknown_isName(const ENCODING *enc, const char *p)
1268 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1269 int c = uenc->convert(uenc->userData, p);
1270 if (c & ~0xFFFF)
1271 return 0;
1272 return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
1275 static int PTRFASTCALL
1276 unknown_isNmstrt(const ENCODING *enc, const char *p)
1278 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1279 int c = uenc->convert(uenc->userData, p);
1280 if (c & ~0xFFFF)
1281 return 0;
1282 return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
1285 static int PTRFASTCALL
1286 unknown_isInvalid(const ENCODING *enc, const char *p)
1288 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1289 int c = uenc->convert(uenc->userData, p);
1290 return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1293 static void PTRCALL
1294 unknown_toUtf8(const ENCODING *enc,
1295 const char **fromP, const char *fromLim,
1296 char **toP, const char *toLim)
1298 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1299 char buf[XML_UTF8_ENCODE_MAX];
1300 for (;;) {
1301 const char *utf8;
1302 int n;
1303 if (*fromP == fromLim)
1304 break;
1305 utf8 = uenc->utf8[(unsigned char)**fromP];
1306 n = *utf8++;
1307 if (n == 0) {
1308 int c = uenc->convert(uenc->userData, *fromP);
1309 n = XmlUtf8Encode(c, buf);
1310 if (n > toLim - *toP)
1311 break;
1312 utf8 = buf;
1313 *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1314 - (BT_LEAD2 - 2));
1316 else {
1317 if (n > toLim - *toP)
1318 break;
1319 (*fromP)++;
1321 do {
1322 *(*toP)++ = *utf8++;
1323 } while (--n != 0);
1327 static void PTRCALL
1328 unknown_toUtf16(const ENCODING *enc,
1329 const char **fromP, const char *fromLim,
1330 unsigned short **toP, const unsigned short *toLim)
1332 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1333 while (*fromP != fromLim && *toP != toLim) {
1334 unsigned short c = uenc->utf16[(unsigned char)**fromP];
1335 if (c == 0) {
1336 c = (unsigned short)
1337 uenc->convert(uenc->userData, *fromP);
1338 *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1339 - (BT_LEAD2 - 2));
1341 else
1342 (*fromP)++;
1343 *(*toP)++ = c;
1347 ENCODING *
1348 XmlInitUnknownEncoding(void *mem,
1349 int *table,
1350 CONVERTER convert,
1351 void *userData)
1353 int i;
1354 struct unknown_encoding *e = (struct unknown_encoding *)mem;
1355 for (i = 0; i < (int)sizeof(struct normal_encoding); i++)
1356 ((char *)mem)[i] = ((char *)&latin1_encoding)[i];
1357 for (i = 0; i < 128; i++)
1358 if (latin1_encoding.type[i] != BT_OTHER
1359 && latin1_encoding.type[i] != BT_NONXML
1360 && table[i] != i)
1361 return 0;
1362 for (i = 0; i < 256; i++) {
1363 int c = table[i];
1364 if (c == -1) {
1365 e->normal.type[i] = BT_MALFORM;
1366 /* This shouldn't really get used. */
1367 e->utf16[i] = 0xFFFF;
1368 e->utf8[i][0] = 1;
1369 e->utf8[i][1] = 0;
1371 else if (c < 0) {
1372 if (c < -4)
1373 return 0;
1374 e->normal.type[i] = (unsigned char)(BT_LEAD2 - (c + 2));
1375 e->utf8[i][0] = 0;
1376 e->utf16[i] = 0;
1378 else if (c < 0x80) {
1379 if (latin1_encoding.type[c] != BT_OTHER
1380 && latin1_encoding.type[c] != BT_NONXML
1381 && c != i)
1382 return 0;
1383 e->normal.type[i] = latin1_encoding.type[c];
1384 e->utf8[i][0] = 1;
1385 e->utf8[i][1] = (char)c;
1386 e->utf16[i] = (unsigned short)(c == 0 ? 0xFFFF : c);
1388 else if (checkCharRefNumber(c) < 0) {
1389 e->normal.type[i] = BT_NONXML;
1390 /* This shouldn't really get used. */
1391 e->utf16[i] = 0xFFFF;
1392 e->utf8[i][0] = 1;
1393 e->utf8[i][1] = 0;
1395 else {
1396 if (c > 0xFFFF)
1397 return 0;
1398 if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
1399 e->normal.type[i] = BT_NMSTRT;
1400 else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
1401 e->normal.type[i] = BT_NAME;
1402 else
1403 e->normal.type[i] = BT_OTHER;
1404 e->utf8[i][0] = (char)XmlUtf8Encode(c, e->utf8[i] + 1);
1405 e->utf16[i] = (unsigned short)c;
1408 e->userData = userData;
1409 e->convert = convert;
1410 if (convert) {
1411 e->normal.isName2 = unknown_isName;
1412 e->normal.isName3 = unknown_isName;
1413 e->normal.isName4 = unknown_isName;
1414 e->normal.isNmstrt2 = unknown_isNmstrt;
1415 e->normal.isNmstrt3 = unknown_isNmstrt;
1416 e->normal.isNmstrt4 = unknown_isNmstrt;
1417 e->normal.isInvalid2 = unknown_isInvalid;
1418 e->normal.isInvalid3 = unknown_isInvalid;
1419 e->normal.isInvalid4 = unknown_isInvalid;
1421 e->normal.enc.utf8Convert = unknown_toUtf8;
1422 e->normal.enc.utf16Convert = unknown_toUtf16;
1423 return &(e->normal.enc);
1426 /* If this enumeration is changed, getEncodingIndex and encodings
1427 must also be changed. */
1428 enum {
1429 UNKNOWN_ENC = -1,
1430 ISO_8859_1_ENC = 0,
1431 US_ASCII_ENC,
1432 UTF_8_ENC,
1433 UTF_16_ENC,
1434 UTF_16BE_ENC,
1435 UTF_16LE_ENC,
1436 /* must match encodingNames up to here */
1437 NO_ENC
1440 static const char KW_ISO_8859_1[] = {
1441 ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8, ASCII_5, ASCII_9,
1442 ASCII_MINUS, ASCII_1, '\0'
1444 static const char KW_US_ASCII[] = {
1445 ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S, ASCII_C, ASCII_I, ASCII_I,
1446 '\0'
1448 static const char KW_UTF_8[] = {
1449 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
1451 static const char KW_UTF_16[] = {
1452 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
1454 static const char KW_UTF_16BE[] = {
1455 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_B, ASCII_E,
1456 '\0'
1458 static const char KW_UTF_16LE[] = {
1459 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_L, ASCII_E,
1460 '\0'
1463 static int FASTCALL
1464 getEncodingIndex(const char *name)
1466 static const char * const encodingNames[] = {
1467 KW_ISO_8859_1,
1468 KW_US_ASCII,
1469 KW_UTF_8,
1470 KW_UTF_16,
1471 KW_UTF_16BE,
1472 KW_UTF_16LE,
1474 int i;
1475 if (name == NULL)
1476 return NO_ENC;
1477 for (i = 0; i < (int)(sizeof(encodingNames)/sizeof(encodingNames[0])); i++)
1478 if (streqci(name, encodingNames[i]))
1479 return i;
1480 return UNKNOWN_ENC;
1483 /* For binary compatibility, we store the index of the encoding
1484 specified at initialization in the isUtf16 member.
1487 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1488 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1490 /* This is what detects the encoding. encodingTable maps from
1491 encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
1492 the external (protocol) specified encoding; state is
1493 XML_CONTENT_STATE if we're parsing an external text entity, and
1494 XML_PROLOG_STATE otherwise.
1498 static int
1499 initScan(const ENCODING * const *encodingTable,
1500 const INIT_ENCODING *enc,
1501 int state,
1502 const char *ptr,
1503 const char *end,
1504 const char **nextTokPtr)
1506 const ENCODING **encPtr;
1508 if (ptr == end)
1509 return XML_TOK_NONE;
1510 encPtr = enc->encPtr;
1511 if (ptr + 1 == end) {
1512 /* only a single byte available for auto-detection */
1513 #ifndef XML_DTD /* FIXME */
1514 /* a well-formed document entity must have more than one byte */
1515 if (state != XML_CONTENT_STATE)
1516 return XML_TOK_PARTIAL;
1517 #endif
1518 /* so we're parsing an external text entity... */
1519 /* if UTF-16 was externally specified, then we need at least 2 bytes */
1520 switch (INIT_ENC_INDEX(enc)) {
1521 case UTF_16_ENC:
1522 case UTF_16LE_ENC:
1523 case UTF_16BE_ENC:
1524 return XML_TOK_PARTIAL;
1526 switch ((unsigned char)*ptr) {
1527 case 0xFE:
1528 case 0xFF:
1529 case 0xEF: /* possibly first byte of UTF-8 BOM */
1530 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1531 && state == XML_CONTENT_STATE)
1532 break;
1533 /* fall through */
1534 case 0x00:
1535 case 0x3C:
1536 return XML_TOK_PARTIAL;
1539 else {
1540 switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
1541 case 0xFEFF:
1542 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1543 && state == XML_CONTENT_STATE)
1544 break;
1545 *nextTokPtr = ptr + 2;
1546 *encPtr = encodingTable[UTF_16BE_ENC];
1547 return XML_TOK_BOM;
1548 /* 00 3C is handled in the default case */
1549 case 0x3C00:
1550 if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
1551 || INIT_ENC_INDEX(enc) == UTF_16_ENC)
1552 && state == XML_CONTENT_STATE)
1553 break;
1554 *encPtr = encodingTable[UTF_16LE_ENC];
1555 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1556 case 0xFFFE:
1557 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1558 && state == XML_CONTENT_STATE)
1559 break;
1560 *nextTokPtr = ptr + 2;
1561 *encPtr = encodingTable[UTF_16LE_ENC];
1562 return XML_TOK_BOM;
1563 case 0xEFBB:
1564 /* Maybe a UTF-8 BOM (EF BB BF) */
1565 /* If there's an explicitly specified (external) encoding
1566 of ISO-8859-1 or some flavour of UTF-16
1567 and this is an external text entity,
1568 don't look for the BOM,
1569 because it might be a legal data.
1571 if (state == XML_CONTENT_STATE) {
1572 int e = INIT_ENC_INDEX(enc);
1573 if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC
1574 || e == UTF_16LE_ENC || e == UTF_16_ENC)
1575 break;
1577 if (ptr + 2 == end)
1578 return XML_TOK_PARTIAL;
1579 if ((unsigned char)ptr[2] == 0xBF) {
1580 *nextTokPtr = ptr + 3;
1581 *encPtr = encodingTable[UTF_8_ENC];
1582 return XML_TOK_BOM;
1584 break;
1585 default:
1586 if (ptr[0] == '\0') {
1587 /* 0 isn't a legal data character. Furthermore a document
1588 entity can only start with ASCII characters. So the only
1589 way this can fail to be big-endian UTF-16 if it it's an
1590 external parsed general entity that's labelled as
1591 UTF-16LE.
1593 if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
1594 break;
1595 *encPtr = encodingTable[UTF_16BE_ENC];
1596 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1598 else if (ptr[1] == '\0') {
1599 /* We could recover here in the case:
1600 - parsing an external entity
1601 - second byte is 0
1602 - no externally specified encoding
1603 - no encoding declaration
1604 by assuming UTF-16LE. But we don't, because this would mean when
1605 presented just with a single byte, we couldn't reliably determine
1606 whether we needed further bytes.
1608 if (state == XML_CONTENT_STATE)
1609 break;
1610 *encPtr = encodingTable[UTF_16LE_ENC];
1611 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1613 break;
1616 *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1617 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1621 #define NS(x) x
1622 #define ns(x) x
1623 #define XML_TOK_NS_C
1624 #include "xmltok_ns.c"
1625 #undef XML_TOK_NS_C
1626 #undef NS
1627 #undef ns
1629 #ifdef XML_NS
1631 #define NS(x) x ## NS
1632 #define ns(x) x ## _ns
1634 #define XML_TOK_NS_C
1635 #include "xmltok_ns.c"
1636 #undef XML_TOK_NS_C
1638 #undef NS
1639 #undef ns
1641 ENCODING *
1642 XmlInitUnknownEncodingNS(void *mem,
1643 int *table,
1644 CONVERTER convert,
1645 void *userData)
1647 ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
1648 if (enc)
1649 ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;
1650 return enc;
1653 #endif /* XML_NS */