1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
5 #ifdef COMPILED_FROM_DSP
7 #elif defined(MACOS_CLASSIC)
10 #include <expat_config.h>
11 #endif /* ndef COMPILED_FROM_DSP */
18 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
20 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
24 { PREFIX(prologTok), PREFIX(contentTok), \
25 PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
26 { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
28 PREFIX(nameMatchesAscii), \
32 PREFIX(charRefNumber), \
33 PREFIX(predefinedEntityName), \
34 PREFIX(updatePosition), \
37 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
39 #define UCS2_GET_NAMING(pages, hi, lo) \
40 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
42 /* A 2 byte UTF-8 representation splits the characters 11 bits between
43 the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
44 pages, 3 bits to add to that index and 5 bits to generate the mask.
46 #define UTF8_GET_NAMING2(pages, byte) \
47 (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
48 + ((((byte)[0]) & 3) << 1) \
49 + ((((byte)[1]) >> 5) & 1)] \
50 & (1 << (((byte)[1]) & 0x1F)))
52 /* A 3 byte UTF-8 representation splits the characters 16 bits between
53 the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
54 into pages, 3 bits to add to that index and 5 bits to generate the
57 #define UTF8_GET_NAMING3(pages, byte) \
58 (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
59 + ((((byte)[1]) >> 2) & 0xF)] \
61 + ((((byte)[1]) & 3) << 1) \
62 + ((((byte)[2]) >> 5) & 1)] \
63 & (1 << (((byte)[2]) & 0x1F)))
65 #define UTF8_GET_NAMING(pages, p, n) \
67 ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
69 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
72 /* Detection of invalid UTF-8 sequences is based on Table 3.1B
73 of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
74 with the additional restriction of not allowing the Unicode
75 code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
76 Implementation details:
77 (A & 0x80) == 0 means A < 0x80
79 (A & 0xC0) == 0xC0 means A > 0xBF
82 #define UTF8_INVALID2(p) \
83 ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
85 #define UTF8_INVALID3(p) \
86 (((p)[2] & 0x80) == 0 \
88 ((*p) == 0xEF && (p)[1] == 0xBF \
92 ((p)[2] & 0xC0) == 0xC0) \
96 (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
98 ((p)[1] & 0x80) == 0 \
100 ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
102 #define UTF8_INVALID4(p) \
103 (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 \
105 ((p)[2] & 0x80) == 0 || ((p)[2] & 0xC0) == 0xC0 \
109 (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
111 ((p)[1] & 0x80) == 0 \
113 ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
116 isNever(const ENCODING *enc, const char *p)
122 utf8_isName2(const ENCODING *enc, const char *p)
124 return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
128 utf8_isName3(const ENCODING *enc, const char *p)
130 return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
133 #define utf8_isName4 isNever
136 utf8_isNmstrt2(const ENCODING *enc, const char *p)
138 return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
142 utf8_isNmstrt3(const ENCODING *enc, const char *p)
144 return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
147 #define utf8_isNmstrt4 isNever
150 utf8_isInvalid2(const ENCODING *enc, const char *p)
152 return UTF8_INVALID2((const unsigned char *)p);
156 utf8_isInvalid3(const ENCODING *enc, const char *p)
158 return UTF8_INVALID3((const unsigned char *)p);
162 utf8_isInvalid4(const ENCODING *enc, const char *p)
164 return UTF8_INVALID4((const unsigned char *)p);
167 struct normal_encoding {
169 unsigned char type[256];
171 int (FASTCALL *byteType)(const ENCODING *, const char *);
172 int (FASTCALL *isNameMin)(const ENCODING *, const char *);
173 int (FASTCALL *isNmstrtMin)(const ENCODING *, const char *);
174 int (FASTCALL *byteToAscii)(const ENCODING *, const char *);
175 int (FASTCALL *charMatches)(const ENCODING *, const char *, int);
176 #endif /* XML_MIN_SIZE */
177 int (FASTCALL *isName2)(const ENCODING *, const char *);
178 int (FASTCALL *isName3)(const ENCODING *, const char *);
179 int (FASTCALL *isName4)(const ENCODING *, const char *);
180 int (FASTCALL *isNmstrt2)(const ENCODING *, const char *);
181 int (FASTCALL *isNmstrt3)(const ENCODING *, const char *);
182 int (FASTCALL *isNmstrt4)(const ENCODING *, const char *);
183 int (FASTCALL *isInvalid2)(const ENCODING *, const char *);
184 int (FASTCALL *isInvalid3)(const ENCODING *, const char *);
185 int (FASTCALL *isInvalid4)(const ENCODING *, const char *);
188 #define AS_NORMAL_ENCODING(enc) ((const struct normal_encoding *) (enc))
192 #define STANDARD_VTABLE(E) \
201 #define STANDARD_VTABLE(E) /* as nothing */
205 #define NORMAL_VTABLE(E) \
216 static int FASTCALL checkCharRefNumber(int);
218 #include "xmltok_impl.h"
222 #define sb_isNameMin isNever
223 #define sb_isNmstrtMin isNever
227 #define MINBPC(enc) ((enc)->minBytesPerChar)
229 /* minimum bytes per character */
230 #define MINBPC(enc) 1
233 #define SB_BYTE_TYPE(enc, p) \
234 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
238 sb_byteType(const ENCODING *enc, const char *p)
240 return SB_BYTE_TYPE(enc, p);
242 #define BYTE_TYPE(enc, p) \
243 (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
245 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
249 #define BYTE_TO_ASCII(enc, p) \
250 (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
252 sb_byteToAscii(const ENCODING *enc, const char *p)
257 #define BYTE_TO_ASCII(enc, p) (*(p))
260 #define IS_NAME_CHAR(enc, p, n) \
261 (AS_NORMAL_ENCODING(enc)->isName ## n(enc, p))
262 #define IS_NMSTRT_CHAR(enc, p, n) \
263 (AS_NORMAL_ENCODING(enc)->isNmstrt ## n(enc, p))
264 #define IS_INVALID_CHAR(enc, p, n) \
265 (AS_NORMAL_ENCODING(enc)->isInvalid ## n(enc, p))
268 #define IS_NAME_CHAR_MINBPC(enc, p) \
269 (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
270 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
271 (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
273 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
274 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
278 #define CHAR_MATCHES(enc, p, c) \
279 (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
281 sb_charMatches(const ENCODING *enc, const char *p, int c)
286 /* c is an ASCII character */
287 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
290 #define PREFIX(ident) normal_ ## ident
291 #include "xmltok_impl.c"
298 #undef IS_NAME_CHAR_MINBPC
299 #undef IS_NMSTRT_CHAR
300 #undef IS_NMSTRT_CHAR_MINBPC
301 #undef IS_INVALID_CHAR
303 enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
311 utf8_toUtf8(const ENCODING *enc,
312 const char **fromP, const char *fromLim,
313 char **toP, const char *toLim)
317 if (fromLim - *fromP > toLim - *toP) {
318 /* Avoid copying partial characters. */
319 for (fromLim = *fromP + (toLim - *toP); fromLim > *fromP; fromLim--)
320 if (((unsigned char)fromLim[-1] & 0xc0) != 0x80)
323 for (to = *toP, from = *fromP; from != fromLim; from++, to++)
330 utf8_toUtf16(const ENCODING *enc,
331 const char **fromP, const char *fromLim,
332 unsigned short **toP, const unsigned short *toLim)
334 unsigned short *to = *toP;
335 const char *from = *fromP;
336 while (from != fromLim && to != toLim) {
337 switch (((struct normal_encoding *)enc)->type[(unsigned char)*from]) {
339 *to++ = (unsigned short)(((from[0] & 0x1f) << 6) | (from[1] & 0x3f));
343 *to++ = (unsigned short)(((from[0] & 0xf) << 12)
344 | ((from[1] & 0x3f) << 6) | (from[2] & 0x3f));
352 n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12)
353 | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
355 to[0] = (unsigned short)((n >> 10) | 0xD800);
356 to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
372 static const struct normal_encoding utf8_encoding_ns = {
373 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
375 #include "asciitab.h"
378 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
382 static const struct normal_encoding utf8_encoding = {
383 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
385 #define BT_COLON BT_NMSTRT
386 #include "asciitab.h"
390 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
395 static const struct normal_encoding internal_utf8_encoding_ns = {
396 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
398 #include "iasciitab.h"
401 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
406 static const struct normal_encoding internal_utf8_encoding = {
407 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
409 #define BT_COLON BT_NMSTRT
410 #include "iasciitab.h"
414 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
418 latin1_toUtf8(const ENCODING *enc,
419 const char **fromP, const char *fromLim,
420 char **toP, const char *toLim)
424 if (*fromP == fromLim)
426 c = (unsigned char)**fromP;
428 if (toLim - *toP < 2)
430 *(*toP)++ = (char)((c >> 6) | UTF8_cval2);
431 *(*toP)++ = (char)((c & 0x3f) | 0x80);
437 *(*toP)++ = *(*fromP)++;
443 latin1_toUtf16(const ENCODING *enc,
444 const char **fromP, const char *fromLim,
445 unsigned short **toP, const unsigned short *toLim)
447 while (*fromP != fromLim && *toP != toLim)
448 *(*toP)++ = (unsigned char)*(*fromP)++;
453 static const struct normal_encoding latin1_encoding_ns = {
454 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
456 #include "asciitab.h"
457 #include "latin1tab.h"
464 static const struct normal_encoding latin1_encoding = {
465 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
467 #define BT_COLON BT_NMSTRT
468 #include "asciitab.h"
470 #include "latin1tab.h"
476 ascii_toUtf8(const ENCODING *enc,
477 const char **fromP, const char *fromLim,
478 char **toP, const char *toLim)
480 while (*fromP != fromLim && *toP != toLim)
481 *(*toP)++ = *(*fromP)++;
486 static const struct normal_encoding ascii_encoding_ns = {
487 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
489 #include "asciitab.h"
497 static const struct normal_encoding ascii_encoding = {
498 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
500 #define BT_COLON BT_NMSTRT
501 #include "asciitab.h"
509 unicode_byte_type(char hi, char lo)
511 switch ((unsigned char)hi) {
512 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
514 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
517 switch ((unsigned char)lo) {
527 #define DEFINE_UTF16_TO_UTF8(E) \
528 static void FASTCALL \
529 E ## toUtf8(const ENCODING *enc, \
530 const char **fromP, const char *fromLim, \
531 char **toP, const char *toLim) \
534 for (from = *fromP; from != fromLim; from += 2) { \
537 unsigned char lo = GET_LO(from); \
538 unsigned char hi = GET_HI(from); \
542 if (*toP == toLim) { \
550 case 0x1: case 0x2: case 0x3: \
551 case 0x4: case 0x5: case 0x6: case 0x7: \
552 if (toLim - *toP < 2) { \
556 *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
557 *(*toP)++ = ((lo & 0x3f) | 0x80); \
560 if (toLim - *toP < 3) { \
564 /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
565 *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
566 *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
567 *(*toP)++ = ((lo & 0x3f) | 0x80); \
569 case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
570 if (toLim - *toP < 4) { \
574 plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
575 *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
576 *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
578 lo2 = GET_LO(from); \
579 *(*toP)++ = (((lo & 0x3) << 4) \
580 | ((GET_HI(from) & 0x3) << 2) \
583 *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
590 #define DEFINE_UTF16_TO_UTF16(E) \
591 static void FASTCALL \
592 E ## toUtf16(const ENCODING *enc, \
593 const char **fromP, const char *fromLim, \
594 unsigned short **toP, const unsigned short *toLim) \
596 /* Avoid copying first half only of surrogate */ \
597 if (fromLim - *fromP > ((toLim - *toP) << 1) \
598 && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
600 for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
601 *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
604 #define SET2(ptr, ch) \
605 (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
606 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
607 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
609 DEFINE_UTF16_TO_UTF8(little2_)
610 DEFINE_UTF16_TO_UTF16(little2_)
616 #define SET2(ptr, ch) \
617 (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
618 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
619 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
621 DEFINE_UTF16_TO_UTF8(big2_)
622 DEFINE_UTF16_TO_UTF16(big2_)
628 #define LITTLE2_BYTE_TYPE(enc, p) \
630 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
631 : unicode_byte_type((p)[1], (p)[0]))
632 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
633 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
634 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
635 UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
636 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
637 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
642 little2_byteType(const ENCODING *enc, const char *p)
644 return LITTLE2_BYTE_TYPE(enc, p);
648 little2_byteToAscii(const ENCODING *enc, const char *p)
650 return LITTLE2_BYTE_TO_ASCII(enc, p);
654 little2_charMatches(const ENCODING *enc, const char *p, int c)
656 return LITTLE2_CHAR_MATCHES(enc, p, c);
660 little2_isNameMin(const ENCODING *enc, const char *p)
662 return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
666 little2_isNmstrtMin(const ENCODING *enc, const char *p)
668 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
672 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
674 #else /* not XML_MIN_SIZE */
677 #define PREFIX(ident) little2_ ## ident
678 #define MINBPC(enc) 2
679 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
680 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
681 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
682 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
683 #define IS_NAME_CHAR(enc, p, n) 0
684 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
685 #define IS_NMSTRT_CHAR(enc, p, n) (0)
686 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
688 #include "xmltok_impl.c"
695 #undef IS_NAME_CHAR_MINBPC
696 #undef IS_NMSTRT_CHAR
697 #undef IS_NMSTRT_CHAR_MINBPC
698 #undef IS_INVALID_CHAR
700 #endif /* not XML_MIN_SIZE */
704 static const struct normal_encoding little2_encoding_ns = {
706 #if BYTEORDER == 1234
713 #include "asciitab.h"
714 #include "latin1tab.h"
716 STANDARD_VTABLE(little2_)
721 static const struct normal_encoding little2_encoding = {
723 #if BYTEORDER == 1234
730 #define BT_COLON BT_NMSTRT
731 #include "asciitab.h"
733 #include "latin1tab.h"
735 STANDARD_VTABLE(little2_)
738 #if BYTEORDER != 4321
742 static const struct normal_encoding internal_little2_encoding_ns = {
745 #include "iasciitab.h"
746 #include "latin1tab.h"
748 STANDARD_VTABLE(little2_)
753 static const struct normal_encoding internal_little2_encoding = {
756 #define BT_COLON BT_NMSTRT
757 #include "iasciitab.h"
759 #include "latin1tab.h"
761 STANDARD_VTABLE(little2_)
767 #define BIG2_BYTE_TYPE(enc, p) \
769 ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
770 : unicode_byte_type((p)[0], (p)[1]))
771 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
772 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
773 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
774 UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
775 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
776 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
781 big2_byteType(const ENCODING *enc, const char *p)
783 return BIG2_BYTE_TYPE(enc, p);
787 big2_byteToAscii(const ENCODING *enc, const char *p)
789 return BIG2_BYTE_TO_ASCII(enc, p);
793 big2_charMatches(const ENCODING *enc, const char *p, int c)
795 return BIG2_CHAR_MATCHES(enc, p, c);
799 big2_isNameMin(const ENCODING *enc, const char *p)
801 return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
805 big2_isNmstrtMin(const ENCODING *enc, const char *p)
807 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
811 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
813 #else /* not XML_MIN_SIZE */
816 #define PREFIX(ident) big2_ ## ident
817 #define MINBPC(enc) 2
818 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
819 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
820 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
821 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
822 #define IS_NAME_CHAR(enc, p, n) 0
823 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
824 #define IS_NMSTRT_CHAR(enc, p, n) (0)
825 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
827 #include "xmltok_impl.c"
834 #undef IS_NAME_CHAR_MINBPC
835 #undef IS_NMSTRT_CHAR
836 #undef IS_NMSTRT_CHAR_MINBPC
837 #undef IS_INVALID_CHAR
839 #endif /* not XML_MIN_SIZE */
843 static const struct normal_encoding big2_encoding_ns = {
845 #if BYTEORDER == 4321
852 #include "asciitab.h"
853 #include "latin1tab.h"
855 STANDARD_VTABLE(big2_)
860 static const struct normal_encoding big2_encoding = {
862 #if BYTEORDER == 4321
869 #define BT_COLON BT_NMSTRT
870 #include "asciitab.h"
872 #include "latin1tab.h"
874 STANDARD_VTABLE(big2_)
877 #if BYTEORDER != 1234
881 static const struct normal_encoding internal_big2_encoding_ns = {
884 #include "iasciitab.h"
885 #include "latin1tab.h"
887 STANDARD_VTABLE(big2_)
892 static const struct normal_encoding internal_big2_encoding = {
895 #define BT_COLON BT_NMSTRT
896 #include "iasciitab.h"
898 #include "latin1tab.h"
900 STANDARD_VTABLE(big2_)
908 streqci(const char *s1, const char *s2)
913 if (ASCII_a <= c1 && c1 <= ASCII_z)
914 c1 += ASCII_A - ASCII_a;
915 if (ASCII_a <= c2 && c2 <= ASCII_z)
916 c2 += ASCII_A - ASCII_a;
926 initUpdatePosition(const ENCODING *enc, const char *ptr,
927 const char *end, POSITION *pos)
929 normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
933 toAscii(const ENCODING *enc, const char *ptr, const char *end)
937 XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
957 /* Return 1 if there's just optional white space or there's an S
958 followed by name=val.
961 parsePseudoAttribute(const ENCODING *enc,
964 const char **namePtr,
965 const char **nameEndPtr,
967 const char **nextTokPtr)
975 if (!isSpace(toAscii(enc, ptr, end))) {
980 ptr += enc->minBytesPerChar;
981 } while (isSpace(toAscii(enc, ptr, end)));
988 c = toAscii(enc, ptr, end);
993 if (c == ASCII_EQUALS) {
1000 ptr += enc->minBytesPerChar;
1001 } while (isSpace(c = toAscii(enc, ptr, end)));
1002 if (c != ASCII_EQUALS) {
1008 ptr += enc->minBytesPerChar;
1010 if (ptr == *namePtr) {
1014 ptr += enc->minBytesPerChar;
1015 c = toAscii(enc, ptr, end);
1016 while (isSpace(c)) {
1017 ptr += enc->minBytesPerChar;
1018 c = toAscii(enc, ptr, end);
1020 if (c != ASCII_QUOT && c != ASCII_APOS) {
1025 ptr += enc->minBytesPerChar;
1027 for (;; ptr += enc->minBytesPerChar) {
1028 c = toAscii(enc, ptr, end);
1031 if (!(ASCII_a <= c && c <= ASCII_z)
1032 && !(ASCII_A <= c && c <= ASCII_Z)
1033 && !(ASCII_0 <= c && c <= ASCII_9)
1034 && c != ASCII_PERIOD
1036 && c != ASCII_UNDERSCORE) {
1041 *nextTokPtr = ptr + enc->minBytesPerChar;
1045 static const char KW_version[] = {
1046 ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
1049 static const char KW_encoding[] = {
1050 ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
1053 static const char KW_standalone[] = {
1054 ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a, ASCII_l, ASCII_o,
1055 ASCII_n, ASCII_e, '\0'
1058 static const char KW_yes[] = {
1059 ASCII_y, ASCII_e, ASCII_s, '\0'
1062 static const char KW_no[] = {
1063 ASCII_n, ASCII_o, '\0'
1067 doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *,
1070 int isGeneralTextEntity,
1071 const ENCODING *enc,
1074 const char **badPtr,
1075 const char **versionPtr,
1076 const char **versionEndPtr,
1077 const char **encodingName,
1078 const ENCODING **encoding,
1081 const char *val = NULL;
1082 const char *name = NULL;
1083 const char *nameEnd = NULL;
1084 ptr += 5 * enc->minBytesPerChar;
1085 end -= 2 * enc->minBytesPerChar;
1086 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)
1091 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1092 if (!isGeneralTextEntity) {
1101 *versionEndPtr = ptr;
1102 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1107 if (isGeneralTextEntity) {
1108 /* a TextDecl must have an EncodingDecl */
1115 if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
1116 int c = toAscii(enc, val, end);
1117 if (!(ASCII_a <= c && c <= ASCII_z) && !(ASCII_A <= c && c <= ASCII_Z)) {
1122 *encodingName = val;
1124 *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
1125 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1132 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone)
1133 || isGeneralTextEntity) {
1137 if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1141 else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1149 while (isSpace(toAscii(enc, ptr, end)))
1150 ptr += enc->minBytesPerChar;
1159 checkCharRefNumber(int result)
1161 switch (result >> 8) {
1162 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1163 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1166 if (latin1_encoding.type[result] == BT_NONXML)
1170 if (result == 0xFFFE || result == 0xFFFF)
1178 XmlUtf8Encode(int c, char *buf)
1181 /* minN is minimum legal resulting value for N byte sequence */
1190 buf[0] = (char)(c | UTF8_cval1);
1194 buf[0] = (char)((c >> 6) | UTF8_cval2);
1195 buf[1] = (char)((c & 0x3f) | 0x80);
1199 buf[0] = (char)((c >> 12) | UTF8_cval3);
1200 buf[1] = (char)(((c >> 6) & 0x3f) | 0x80);
1201 buf[2] = (char)((c & 0x3f) | 0x80);
1205 buf[0] = (char)((c >> 18) | UTF8_cval4);
1206 buf[1] = (char)(((c >> 12) & 0x3f) | 0x80);
1207 buf[2] = (char)(((c >> 6) & 0x3f) | 0x80);
1208 buf[3] = (char)((c & 0x3f) | 0x80);
1215 XmlUtf16Encode(int charNum, unsigned short *buf)
1219 if (charNum < 0x10000) {
1220 buf[0] = (unsigned short)charNum;
1223 if (charNum < 0x110000) {
1225 buf[0] = (unsigned short)((charNum >> 10) + 0xD800);
1226 buf[1] = (unsigned short)((charNum & 0x3FF) + 0xDC00);
1232 struct unknown_encoding {
1233 struct normal_encoding normal;
1234 int (*convert)(void *userData, const char *p);
1236 unsigned short utf16[256];
1240 #define AS_UNKNOWN_ENCODING(enc) ((const struct unknown_encoding *) (enc))
1243 XmlSizeOfUnknownEncoding(void)
1245 return sizeof(struct unknown_encoding);
1249 unknown_isName(const ENCODING *enc, const char *p)
1251 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1252 int c = uenc->convert(uenc->userData, p);
1255 return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
1259 unknown_isNmstrt(const ENCODING *enc, const char *p)
1261 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1262 int c = uenc->convert(uenc->userData, p);
1265 return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
1269 unknown_isInvalid(const ENCODING *enc, const char *p)
1271 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1272 int c = uenc->convert(uenc->userData, p);
1273 return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1276 static void FASTCALL
1277 unknown_toUtf8(const ENCODING *enc,
1278 const char **fromP, const char *fromLim,
1279 char **toP, const char *toLim)
1281 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1282 char buf[XML_UTF8_ENCODE_MAX];
1286 if (*fromP == fromLim)
1288 utf8 = uenc->utf8[(unsigned char)**fromP];
1291 int c = uenc->convert(uenc->userData, *fromP);
1292 n = XmlUtf8Encode(c, buf);
1293 if (n > toLim - *toP)
1296 *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1300 if (n > toLim - *toP)
1305 *(*toP)++ = *utf8++;
1310 static void FASTCALL
1311 unknown_toUtf16(const ENCODING *enc,
1312 const char **fromP, const char *fromLim,
1313 unsigned short **toP, const unsigned short *toLim)
1315 const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1316 while (*fromP != fromLim && *toP != toLim) {
1317 unsigned short c = uenc->utf16[(unsigned char)**fromP];
1319 c = (unsigned short)
1320 uenc->convert(uenc->userData, *fromP);
1321 *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1331 XmlInitUnknownEncoding(void *mem,
1333 int (*convert)(void *userData, const char *p),
1337 struct unknown_encoding *e = mem;
1338 for (i = 0; i < (int)sizeof(struct normal_encoding); i++)
1339 ((char *)mem)[i] = ((char *)&latin1_encoding)[i];
1340 for (i = 0; i < 128; i++)
1341 if (latin1_encoding.type[i] != BT_OTHER
1342 && latin1_encoding.type[i] != BT_NONXML
1345 for (i = 0; i < 256; i++) {
1348 e->normal.type[i] = BT_MALFORM;
1349 /* This shouldn't really get used. */
1350 e->utf16[i] = 0xFFFF;
1357 e->normal.type[i] = (unsigned char)(BT_LEAD2 - (c + 2));
1361 else if (c < 0x80) {
1362 if (latin1_encoding.type[c] != BT_OTHER
1363 && latin1_encoding.type[c] != BT_NONXML
1366 e->normal.type[i] = latin1_encoding.type[c];
1368 e->utf8[i][1] = (char)c;
1369 e->utf16[i] = (unsigned short)(c == 0 ? 0xFFFF : c);
1371 else if (checkCharRefNumber(c) < 0) {
1372 e->normal.type[i] = BT_NONXML;
1373 /* This shouldn't really get used. */
1374 e->utf16[i] = 0xFFFF;
1381 if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
1382 e->normal.type[i] = BT_NMSTRT;
1383 else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
1384 e->normal.type[i] = BT_NAME;
1386 e->normal.type[i] = BT_OTHER;
1387 e->utf8[i][0] = (char)XmlUtf8Encode(c, e->utf8[i] + 1);
1388 e->utf16[i] = (unsigned short)c;
1391 e->userData = userData;
1392 e->convert = convert;
1394 e->normal.isName2 = unknown_isName;
1395 e->normal.isName3 = unknown_isName;
1396 e->normal.isName4 = unknown_isName;
1397 e->normal.isNmstrt2 = unknown_isNmstrt;
1398 e->normal.isNmstrt3 = unknown_isNmstrt;
1399 e->normal.isNmstrt4 = unknown_isNmstrt;
1400 e->normal.isInvalid2 = unknown_isInvalid;
1401 e->normal.isInvalid3 = unknown_isInvalid;
1402 e->normal.isInvalid4 = unknown_isInvalid;
1404 e->normal.enc.utf8Convert = unknown_toUtf8;
1405 e->normal.enc.utf16Convert = unknown_toUtf16;
1406 return &(e->normal.enc);
1409 /* If this enumeration is changed, getEncodingIndex and encodings
1410 must also be changed. */
1419 /* must match encodingNames up to here */
1423 static const char KW_ISO_8859_1[] = {
1424 ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8, ASCII_5, ASCII_9,
1425 ASCII_MINUS, ASCII_1, '\0'
1427 static const char KW_US_ASCII[] = {
1428 ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S, ASCII_C, ASCII_I, ASCII_I,
1431 static const char KW_UTF_8[] = {
1432 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
1434 static const char KW_UTF_16[] = {
1435 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
1437 static const char KW_UTF_16BE[] = {
1438 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_B, ASCII_E,
1441 static const char KW_UTF_16LE[] = {
1442 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_L, ASCII_E,
1447 getEncodingIndex(const char *name)
1449 static const char *encodingNames[] = {
1460 for (i = 0; i < (int)(sizeof(encodingNames)/sizeof(encodingNames[0])); i++)
1461 if (streqci(name, encodingNames[i]))
1466 /* For binary compatibility, we store the index of the encoding
1467 specified at initialization in the isUtf16 member.
1470 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1471 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1473 /* This is what detects the encoding. encodingTable maps from
1474 encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
1475 the external (protocol) specified encoding; state is
1476 XML_CONTENT_STATE if we're parsing an external text entity, and
1477 XML_PROLOG_STATE otherwise.
1482 initScan(const ENCODING **encodingTable,
1483 const INIT_ENCODING *enc,
1487 const char **nextTokPtr)
1489 const ENCODING **encPtr;
1492 return XML_TOK_NONE;
1493 encPtr = enc->encPtr;
1494 if (ptr + 1 == end) {
1495 /* only a single byte available for auto-detection */
1496 #ifndef XML_DTD /* FIXME */
1497 /* a well-formed document entity must have more than one byte */
1498 if (state != XML_CONTENT_STATE)
1499 return XML_TOK_PARTIAL;
1501 /* so we're parsing an external text entity... */
1502 /* if UTF-16 was externally specified, then we need at least 2 bytes */
1503 switch (INIT_ENC_INDEX(enc)) {
1507 return XML_TOK_PARTIAL;
1509 switch ((unsigned char)*ptr) {
1512 case 0xEF: /* possibly first byte of UTF-8 BOM */
1513 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1514 && state == XML_CONTENT_STATE)
1519 return XML_TOK_PARTIAL;
1523 switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
1525 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1526 && state == XML_CONTENT_STATE)
1528 *nextTokPtr = ptr + 2;
1529 *encPtr = encodingTable[UTF_16BE_ENC];
1531 /* 00 3C is handled in the default case */
1533 if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
1534 || INIT_ENC_INDEX(enc) == UTF_16_ENC)
1535 && state == XML_CONTENT_STATE)
1537 *encPtr = encodingTable[UTF_16LE_ENC];
1538 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1540 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1541 && state == XML_CONTENT_STATE)
1543 *nextTokPtr = ptr + 2;
1544 *encPtr = encodingTable[UTF_16LE_ENC];
1547 /* Maybe a UTF-8 BOM (EF BB BF) */
1548 /* If there's an explicitly specified (external) encoding
1549 of ISO-8859-1 or some flavour of UTF-16
1550 and this is an external text entity,
1551 don't look for the BOM,
1552 because it might be a legal data.
1554 if (state == XML_CONTENT_STATE) {
1555 int e = INIT_ENC_INDEX(enc);
1556 if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC
1557 || e == UTF_16LE_ENC || e == UTF_16_ENC)
1561 return XML_TOK_PARTIAL;
1562 if ((unsigned char)ptr[2] == 0xBF) {
1563 *nextTokPtr = ptr + 3;
1564 *encPtr = encodingTable[UTF_8_ENC];
1569 if (ptr[0] == '\0') {
1570 /* 0 isn't a legal data character. Furthermore a document
1571 entity can only start with ASCII characters. So the only
1572 way this can fail to be big-endian UTF-16 if it it's an
1573 external parsed general entity that's labelled as
1576 if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
1578 *encPtr = encodingTable[UTF_16BE_ENC];
1579 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1581 else if (ptr[1] == '\0') {
1582 /* We could recover here in the case:
1583 - parsing an external entity
1585 - no externally specified encoding
1586 - no encoding declaration
1587 by assuming UTF-16LE. But we don't, because this would mean when
1588 presented just with a single byte, we couldn't reliably determine
1589 whether we needed further bytes.
1591 if (state == XML_CONTENT_STATE)
1593 *encPtr = encodingTable[UTF_16LE_ENC];
1594 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1599 *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1600 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1606 #include "xmltok_ns.c"
1612 #define NS(x) x ## NS
1613 #define ns(x) x ## _ns
1615 #include "xmltok_ns.c"
1621 XmlInitUnknownEncodingNS(void *mem,
1623 int (*convert)(void *userData, const char *p),
1626 ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
1628 ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;