1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
7 #ifdef COMPILED_FROM_DSP
9 #elif defined(MACOS_CLASSIC)
10 #include "macconfig.h"
11 #elif defined(__amigaos4__)
12 #include "amigaconfig.h"
14 #ifdef HAVE_EXPAT_CONFIG_H
15 #include <expat_config.h>
17 #endif /* ndef COMPILED_FROM_DSP */
19 #include "expat_external.h"
25 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
27 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
31 { PREFIX(prologTok), PREFIX(contentTok), \
32 PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
33 { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
35 PREFIX(nameMatchesAscii), \
39 PREFIX(charRefNumber), \
40 PREFIX(predefinedEntityName), \
41 PREFIX(updatePosition), \
44 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
46 #define UCS2_GET_NAMING(pages, hi, lo) \
47 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
49 /* A 2 byte UTF-8 representation splits the characters 11 bits between
50 the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
51 pages, 3 bits to add to that index and 5 bits to generate the mask.
53 #define UTF8_GET_NAMING2(pages, byte) \
54 (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
55 + ((((byte)[0]) & 3) << 1) \
56 + ((((byte)[1]) >> 5) & 1)] \
57 & (1 << (((byte)[1]) & 0x1F)))
59 /* A 3 byte UTF-8 representation splits the characters 16 bits between
60 the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
61 into pages, 3 bits to add to that index and 5 bits to generate the
64 #define UTF8_GET_NAMING3(pages, byte) \
65 (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
66 + ((((byte)[1]) >> 2) & 0xF)] \
68 + ((((byte)[1]) & 3) << 1) \
69 + ((((byte)[2]) >> 5) & 1)] \
70 & (1 << (((byte)[2]) & 0x1F)))
72 #define UTF8_GET_NAMING(pages, p, n) \
74 ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
76 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
79 /* Detection of invalid UTF-8 sequences is based on Table 3.1B
80 of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
81 with the additional restriction of not allowing the Unicode
82 code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
83 Implementation details:
84 (A & 0x80) == 0 means A < 0x80
86 (A & 0xC0) == 0xC0 means A > 0xBF
89 #define UTF8_INVALID2(p) \
90 ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
92 #define UTF8_INVALID3(p) \
93 (((p)[2] & 0x80) == 0 \
95 ((*p) == 0xEF && (p)[1] == 0xBF \
99 ((p)[2] & 0xC0) == 0xC0) \
103 (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
105 ((p)[1] & 0x80) == 0 \
107 ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
109 #define UTF8_INVALID4(p) \
110 (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 \
112 ((p)[2] & 0x80) == 0 || ((p)[2] & 0xC0) == 0xC0 \
116 (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
118 ((p)[1] & 0x80) == 0 \
120 ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
122 static int PTRFASTCALL
123 isNever(const ENCODING
*enc
, const char *p
)
128 static int PTRFASTCALL
129 utf8_isName2(const ENCODING
*enc
, const char *p
)
131 return UTF8_GET_NAMING2(namePages
, (const unsigned char *)p
);
134 static int PTRFASTCALL
135 utf8_isName3(const ENCODING
*enc
, const char *p
)
137 return UTF8_GET_NAMING3(namePages
, (const unsigned char *)p
);
140 #define utf8_isName4 isNever
142 static int PTRFASTCALL
143 utf8_isNmstrt2(const ENCODING
*enc
, const char *p
)
145 return UTF8_GET_NAMING2(nmstrtPages
, (const unsigned char *)p
);
148 static int PTRFASTCALL
149 utf8_isNmstrt3(const ENCODING
*enc
, const char *p
)
151 return UTF8_GET_NAMING3(nmstrtPages
, (const unsigned char *)p
);
154 #define utf8_isNmstrt4 isNever
156 static int PTRFASTCALL
157 utf8_isInvalid2(const ENCODING
*enc
, const char *p
)
159 return UTF8_INVALID2((const unsigned char *)p
);
162 static int PTRFASTCALL
163 utf8_isInvalid3(const ENCODING
*enc
, const char *p
)
165 return UTF8_INVALID3((const unsigned char *)p
);
168 static int PTRFASTCALL
169 utf8_isInvalid4(const ENCODING
*enc
, const char *p
)
171 return UTF8_INVALID4((const unsigned char *)p
);
174 struct normal_encoding
{
176 unsigned char type
[256];
178 int (PTRFASTCALL
*byteType
)(const ENCODING
*, const char *);
179 int (PTRFASTCALL
*isNameMin
)(const ENCODING
*, const char *);
180 int (PTRFASTCALL
*isNmstrtMin
)(const ENCODING
*, const char *);
181 int (PTRFASTCALL
*byteToAscii
)(const ENCODING
*, const char *);
182 int (PTRCALL
*charMatches
)(const ENCODING
*, const char *, int);
183 #endif /* XML_MIN_SIZE */
184 int (PTRFASTCALL
*isName2
)(const ENCODING
*, const char *);
185 int (PTRFASTCALL
*isName3
)(const ENCODING
*, const char *);
186 int (PTRFASTCALL
*isName4
)(const ENCODING
*, const char *);
187 int (PTRFASTCALL
*isNmstrt2
)(const ENCODING
*, const char *);
188 int (PTRFASTCALL
*isNmstrt3
)(const ENCODING
*, const char *);
189 int (PTRFASTCALL
*isNmstrt4
)(const ENCODING
*, const char *);
190 int (PTRFASTCALL
*isInvalid2
)(const ENCODING
*, const char *);
191 int (PTRFASTCALL
*isInvalid3
)(const ENCODING
*, const char *);
192 int (PTRFASTCALL
*isInvalid4
)(const ENCODING
*, const char *);
195 #define AS_NORMAL_ENCODING(enc) ((const struct normal_encoding *) (enc))
199 #define STANDARD_VTABLE(E) \
208 #define STANDARD_VTABLE(E) /* as nothing */
212 #define NORMAL_VTABLE(E) \
223 static int FASTCALL
checkCharRefNumber(int);
225 #include "xmltok_impl.h"
229 #define sb_isNameMin isNever
230 #define sb_isNmstrtMin isNever
234 #define MINBPC(enc) ((enc)->minBytesPerChar)
236 /* minimum bytes per character */
237 #define MINBPC(enc) 1
240 #define SB_BYTE_TYPE(enc, p) \
241 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
244 static int PTRFASTCALL
245 sb_byteType(const ENCODING
*enc
, const char *p
)
247 return SB_BYTE_TYPE(enc
, p
);
249 #define BYTE_TYPE(enc, p) \
250 (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
252 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
256 #define BYTE_TO_ASCII(enc, p) \
257 (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
258 static int PTRFASTCALL
259 sb_byteToAscii(const ENCODING
*enc
, const char *p
)
264 #define BYTE_TO_ASCII(enc, p) (*(p))
267 #define IS_NAME_CHAR(enc, p, n) \
268 (AS_NORMAL_ENCODING(enc)->isName ## n(enc, p))
269 #define IS_NMSTRT_CHAR(enc, p, n) \
270 (AS_NORMAL_ENCODING(enc)->isNmstrt ## n(enc, p))
271 #define IS_INVALID_CHAR(enc, p, n) \
272 (AS_NORMAL_ENCODING(enc)->isInvalid ## n(enc, p))
275 #define IS_NAME_CHAR_MINBPC(enc, p) \
276 (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
277 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
278 (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
280 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
281 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
285 #define CHAR_MATCHES(enc, p, c) \
286 (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
288 sb_charMatches(const ENCODING
*enc
, const char *p
, int c
)
293 /* c is an ASCII character */
294 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
297 #define PREFIX(ident) normal_ ## ident
298 #include "xmltok_impl.c"
305 #undef IS_NAME_CHAR_MINBPC
306 #undef IS_NMSTRT_CHAR
307 #undef IS_NMSTRT_CHAR_MINBPC
308 #undef IS_INVALID_CHAR
310 enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
318 utf8_toUtf8(const ENCODING
*enc
,
319 const char **fromP
, const char *fromLim
,
320 char **toP
, const char *toLim
)
324 if (fromLim
- *fromP
> toLim
- *toP
) {
325 /* Avoid copying partial characters. */
326 for (fromLim
= *fromP
+ (toLim
- *toP
); fromLim
> *fromP
; fromLim
--)
327 if (((unsigned char)fromLim
[-1] & 0xc0) != 0x80)
330 for (to
= *toP
, from
= *fromP
; from
!= fromLim
; from
++, to
++)
337 utf8_toUtf16(const ENCODING
*enc
,
338 const char **fromP
, const char *fromLim
,
339 unsigned short **toP
, const unsigned short *toLim
)
341 unsigned short *to
= *toP
;
342 const char *from
= *fromP
;
343 while (from
!= fromLim
&& to
!= toLim
) {
344 switch (((struct normal_encoding
*)enc
)->type
[(unsigned char)*from
]) {
346 *to
++ = (unsigned short)(((from
[0] & 0x1f) << 6) | (from
[1] & 0x3f));
350 *to
++ = (unsigned short)(((from
[0] & 0xf) << 12)
351 | ((from
[1] & 0x3f) << 6) | (from
[2] & 0x3f));
359 n
= ((from
[0] & 0x7) << 18) | ((from
[1] & 0x3f) << 12)
360 | ((from
[2] & 0x3f) << 6) | (from
[3] & 0x3f);
362 to
[0] = (unsigned short)((n
>> 10) | 0xD800);
363 to
[1] = (unsigned short)((n
& 0x3FF) | 0xDC00);
379 static const struct normal_encoding utf8_encoding_ns
= {
380 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
382 #include "asciitab.h"
385 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
389 static const struct normal_encoding utf8_encoding
= {
390 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
392 #define BT_COLON BT_NMSTRT
393 #include "asciitab.h"
397 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
402 static const struct normal_encoding internal_utf8_encoding_ns
= {
403 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
405 #include "iasciitab.h"
408 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
413 static const struct normal_encoding internal_utf8_encoding
= {
414 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
416 #define BT_COLON BT_NMSTRT
417 #include "iasciitab.h"
421 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
425 latin1_toUtf8(const ENCODING
*enc
,
426 const char **fromP
, const char *fromLim
,
427 char **toP
, const char *toLim
)
431 if (*fromP
== fromLim
)
433 c
= (unsigned char)**fromP
;
435 if (toLim
- *toP
< 2)
437 *(*toP
)++ = (char)((c
>> 6) | UTF8_cval2
);
438 *(*toP
)++ = (char)((c
& 0x3f) | 0x80);
444 *(*toP
)++ = *(*fromP
)++;
450 latin1_toUtf16(const ENCODING
*enc
,
451 const char **fromP
, const char *fromLim
,
452 unsigned short **toP
, const unsigned short *toLim
)
454 while (*fromP
!= fromLim
&& *toP
!= toLim
)
455 *(*toP
)++ = (unsigned char)*(*fromP
)++;
460 static const struct normal_encoding latin1_encoding_ns
= {
461 { VTABLE1
, latin1_toUtf8
, latin1_toUtf16
, 1, 0, 0 },
463 #include "asciitab.h"
464 #include "latin1tab.h"
471 static const struct normal_encoding latin1_encoding
= {
472 { VTABLE1
, latin1_toUtf8
, latin1_toUtf16
, 1, 0, 0 },
474 #define BT_COLON BT_NMSTRT
475 #include "asciitab.h"
477 #include "latin1tab.h"
483 ascii_toUtf8(const ENCODING
*enc
,
484 const char **fromP
, const char *fromLim
,
485 char **toP
, const char *toLim
)
487 while (*fromP
!= fromLim
&& *toP
!= toLim
)
488 *(*toP
)++ = *(*fromP
)++;
493 static const struct normal_encoding ascii_encoding_ns
= {
494 { VTABLE1
, ascii_toUtf8
, latin1_toUtf16
, 1, 1, 0 },
496 #include "asciitab.h"
504 static const struct normal_encoding ascii_encoding
= {
505 { VTABLE1
, ascii_toUtf8
, latin1_toUtf16
, 1, 1, 0 },
507 #define BT_COLON BT_NMSTRT
508 #include "asciitab.h"
515 static int PTRFASTCALL
516 unicode_byte_type(char hi
, char lo
)
518 switch ((unsigned char)hi
) {
519 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
521 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
524 switch ((unsigned char)lo
) {
534 #define DEFINE_UTF16_TO_UTF8(E) \
535 static void PTRCALL \
536 E ## toUtf8(const ENCODING *enc, \
537 const char **fromP, const char *fromLim, \
538 char **toP, const char *toLim) \
541 for (from = *fromP; from != fromLim; from += 2) { \
544 unsigned char lo = GET_LO(from); \
545 unsigned char hi = GET_HI(from); \
549 if (*toP == toLim) { \
557 case 0x1: case 0x2: case 0x3: \
558 case 0x4: case 0x5: case 0x6: case 0x7: \
559 if (toLim - *toP < 2) { \
563 *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
564 *(*toP)++ = ((lo & 0x3f) | 0x80); \
567 if (toLim - *toP < 3) { \
571 /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
572 *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
573 *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
574 *(*toP)++ = ((lo & 0x3f) | 0x80); \
576 case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
577 if (toLim - *toP < 4) { \
581 plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
582 *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
583 *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
585 lo2 = GET_LO(from); \
586 *(*toP)++ = (((lo & 0x3) << 4) \
587 | ((GET_HI(from) & 0x3) << 2) \
590 *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
597 #define DEFINE_UTF16_TO_UTF16(E) \
598 static void PTRCALL \
599 E ## toUtf16(const ENCODING *enc, \
600 const char **fromP, const char *fromLim, \
601 unsigned short **toP, const unsigned short *toLim) \
603 /* Avoid copying first half only of surrogate */ \
604 if (fromLim - *fromP > ((toLim - *toP) << 1) \
605 && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
607 for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
608 *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
611 #define SET2(ptr, ch) \
612 (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
613 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
614 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
616 DEFINE_UTF16_TO_UTF8(little2_
)
617 DEFINE_UTF16_TO_UTF16(little2_
)
623 #define SET2(ptr, ch) \
624 (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
625 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
626 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
628 DEFINE_UTF16_TO_UTF8(big2_
)
629 DEFINE_UTF16_TO_UTF16(big2_
)
635 #define LITTLE2_BYTE_TYPE(enc, p) \
637 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
638 : unicode_byte_type((p)[1], (p)[0]))
639 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
640 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
641 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
642 UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
643 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
644 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
648 static int PTRFASTCALL
649 little2_byteType(const ENCODING
*enc
, const char *p
)
651 return LITTLE2_BYTE_TYPE(enc
, p
);
654 static int PTRFASTCALL
655 little2_byteToAscii(const ENCODING
*enc
, const char *p
)
657 return LITTLE2_BYTE_TO_ASCII(enc
, p
);
661 little2_charMatches(const ENCODING
*enc
, const char *p
, int c
)
663 return LITTLE2_CHAR_MATCHES(enc
, p
, c
);
666 static int PTRFASTCALL
667 little2_isNameMin(const ENCODING
*enc
, const char *p
)
669 return LITTLE2_IS_NAME_CHAR_MINBPC(enc
, p
);
672 static int PTRFASTCALL
673 little2_isNmstrtMin(const ENCODING
*enc
, const char *p
)
675 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc
, p
);
679 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
681 #else /* not XML_MIN_SIZE */
684 #define PREFIX(ident) little2_ ## ident
685 #define MINBPC(enc) 2
686 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
687 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
688 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
689 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
690 #define IS_NAME_CHAR(enc, p, n) 0
691 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
692 #define IS_NMSTRT_CHAR(enc, p, n) (0)
693 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
695 #include "xmltok_impl.c"
702 #undef IS_NAME_CHAR_MINBPC
703 #undef IS_NMSTRT_CHAR
704 #undef IS_NMSTRT_CHAR_MINBPC
705 #undef IS_INVALID_CHAR
707 #endif /* not XML_MIN_SIZE */
711 static const struct normal_encoding little2_encoding_ns
= {
713 #if BYTEORDER == 1234
720 #include "asciitab.h"
721 #include "latin1tab.h"
723 STANDARD_VTABLE(little2_
)
728 static const struct normal_encoding little2_encoding
= {
730 #if BYTEORDER == 1234
737 #define BT_COLON BT_NMSTRT
738 #include "asciitab.h"
740 #include "latin1tab.h"
742 STANDARD_VTABLE(little2_
)
745 #if BYTEORDER != 4321
749 static const struct normal_encoding internal_little2_encoding_ns
= {
752 #include "iasciitab.h"
753 #include "latin1tab.h"
755 STANDARD_VTABLE(little2_
)
760 static const struct normal_encoding internal_little2_encoding
= {
763 #define BT_COLON BT_NMSTRT
764 #include "iasciitab.h"
766 #include "latin1tab.h"
768 STANDARD_VTABLE(little2_
)
774 #define BIG2_BYTE_TYPE(enc, p) \
776 ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
777 : unicode_byte_type((p)[0], (p)[1]))
778 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
779 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
780 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
781 UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
782 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
783 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
787 static int PTRFASTCALL
788 big2_byteType(const ENCODING
*enc
, const char *p
)
790 return BIG2_BYTE_TYPE(enc
, p
);
793 static int PTRFASTCALL
794 big2_byteToAscii(const ENCODING
*enc
, const char *p
)
796 return BIG2_BYTE_TO_ASCII(enc
, p
);
800 big2_charMatches(const ENCODING
*enc
, const char *p
, int c
)
802 return BIG2_CHAR_MATCHES(enc
, p
, c
);
805 static int PTRFASTCALL
806 big2_isNameMin(const ENCODING
*enc
, const char *p
)
808 return BIG2_IS_NAME_CHAR_MINBPC(enc
, p
);
811 static int PTRFASTCALL
812 big2_isNmstrtMin(const ENCODING
*enc
, const char *p
)
814 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc
, p
);
818 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
820 #else /* not XML_MIN_SIZE */
823 #define PREFIX(ident) big2_ ## ident
824 #define MINBPC(enc) 2
825 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
826 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
827 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
828 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
829 #define IS_NAME_CHAR(enc, p, n) 0
830 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
831 #define IS_NMSTRT_CHAR(enc, p, n) (0)
832 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
834 #include "xmltok_impl.c"
841 #undef IS_NAME_CHAR_MINBPC
842 #undef IS_NMSTRT_CHAR
843 #undef IS_NMSTRT_CHAR_MINBPC
844 #undef IS_INVALID_CHAR
846 #endif /* not XML_MIN_SIZE */
850 static const struct normal_encoding big2_encoding_ns
= {
852 #if BYTEORDER == 4321
859 #include "asciitab.h"
860 #include "latin1tab.h"
862 STANDARD_VTABLE(big2_
)
867 static const struct normal_encoding big2_encoding
= {
869 #if BYTEORDER == 4321
876 #define BT_COLON BT_NMSTRT
877 #include "asciitab.h"
879 #include "latin1tab.h"
881 STANDARD_VTABLE(big2_
)
884 #if BYTEORDER != 1234
888 static const struct normal_encoding internal_big2_encoding_ns
= {
891 #include "iasciitab.h"
892 #include "latin1tab.h"
894 STANDARD_VTABLE(big2_
)
899 static const struct normal_encoding internal_big2_encoding
= {
902 #define BT_COLON BT_NMSTRT
903 #include "iasciitab.h"
905 #include "latin1tab.h"
907 STANDARD_VTABLE(big2_
)
915 streqci(const char *s1
, const char *s2
)
920 if (ASCII_a
<= c1
&& c1
<= ASCII_z
)
921 c1
+= ASCII_A
- ASCII_a
;
922 if (ASCII_a
<= c2
&& c2
<= ASCII_z
)
923 c2
+= ASCII_A
- ASCII_a
;
933 initUpdatePosition(const ENCODING
*enc
, const char *ptr
,
934 const char *end
, POSITION
*pos
)
936 normal_updatePosition(&utf8_encoding
.enc
, ptr
, end
, pos
);
940 toAscii(const ENCODING
*enc
, const char *ptr
, const char *end
)
944 XmlUtf8Convert(enc
, &ptr
, end
, &p
, p
+ 1);
964 /* Return 1 if there's just optional white space or there's an S
965 followed by name=val.
968 parsePseudoAttribute(const ENCODING
*enc
,
971 const char **namePtr
,
972 const char **nameEndPtr
,
974 const char **nextTokPtr
)
982 if (!isSpace(toAscii(enc
, ptr
, end
))) {
987 ptr
+= enc
->minBytesPerChar
;
988 } while (isSpace(toAscii(enc
, ptr
, end
)));
995 c
= toAscii(enc
, ptr
, end
);
1000 if (c
== ASCII_EQUALS
) {
1007 ptr
+= enc
->minBytesPerChar
;
1008 } while (isSpace(c
= toAscii(enc
, ptr
, end
)));
1009 if (c
!= ASCII_EQUALS
) {
1015 ptr
+= enc
->minBytesPerChar
;
1017 if (ptr
== *namePtr
) {
1021 ptr
+= enc
->minBytesPerChar
;
1022 c
= toAscii(enc
, ptr
, end
);
1023 while (isSpace(c
)) {
1024 ptr
+= enc
->minBytesPerChar
;
1025 c
= toAscii(enc
, ptr
, end
);
1027 if (c
!= ASCII_QUOT
&& c
!= ASCII_APOS
) {
1032 ptr
+= enc
->minBytesPerChar
;
1034 for (;; ptr
+= enc
->minBytesPerChar
) {
1035 c
= toAscii(enc
, ptr
, end
);
1038 if (!(ASCII_a
<= c
&& c
<= ASCII_z
)
1039 && !(ASCII_A
<= c
&& c
<= ASCII_Z
)
1040 && !(ASCII_0
<= c
&& c
<= ASCII_9
)
1041 && c
!= ASCII_PERIOD
1043 && c
!= ASCII_UNDERSCORE
) {
1048 *nextTokPtr
= ptr
+ enc
->minBytesPerChar
;
1052 static const char KW_version
[] = {
1053 ASCII_v
, ASCII_e
, ASCII_r
, ASCII_s
, ASCII_i
, ASCII_o
, ASCII_n
, '\0'
1056 static const char KW_encoding
[] = {
1057 ASCII_e
, ASCII_n
, ASCII_c
, ASCII_o
, ASCII_d
, ASCII_i
, ASCII_n
, ASCII_g
, '\0'
1060 static const char KW_standalone
[] = {
1061 ASCII_s
, ASCII_t
, ASCII_a
, ASCII_n
, ASCII_d
, ASCII_a
, ASCII_l
, ASCII_o
,
1062 ASCII_n
, ASCII_e
, '\0'
1065 static const char KW_yes
[] = {
1066 ASCII_y
, ASCII_e
, ASCII_s
, '\0'
1069 static const char KW_no
[] = {
1070 ASCII_n
, ASCII_o
, '\0'
1073 /* BEGIN MOZILLA CHANGE (http://bugzilla.mozilla.org/show_bug.cgi?id=62157) */
1074 static const char KW_XML_1_0
[] = {
1075 ASCII_1
, ASCII_PERIOD
, ASCII_0
, '\0'
1077 /* END MOZILLA CHANGE */
1080 doParseXmlDecl(const ENCODING
*(*encodingFinder
)(const ENCODING
*,
1083 int isGeneralTextEntity
,
1084 const ENCODING
*enc
,
1087 const char **badPtr
,
1088 const char **versionPtr
,
1089 const char **versionEndPtr
,
1090 const char **encodingName
,
1091 const ENCODING
**encoding
,
1094 const char *val
= NULL
;
1095 const char *name
= NULL
;
1096 const char *nameEnd
= NULL
;
1097 ptr
+= 5 * enc
->minBytesPerChar
;
1098 end
-= 2 * enc
->minBytesPerChar
;
1099 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)
1104 if (!XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_version
)) {
1105 if (!isGeneralTextEntity
) {
1114 *versionEndPtr
= ptr
;
1115 /* BEGIN MOZILLA CHANGE (http://bugzilla.mozilla.org/show_bug.cgi?id=62157) */
1116 /* Anything else but a version="1.0" is invalid for us, until we support later versions. */
1117 if (!XmlNameMatchesAscii(enc
, val
, ptr
- enc
->minBytesPerChar
, KW_XML_1_0
)) {
1121 /* END MOZILLA CHANGE */
1122 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)) {
1127 if (isGeneralTextEntity
) {
1128 /* a TextDecl must have an EncodingDecl */
1135 if (XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_encoding
)) {
1136 int c
= toAscii(enc
, val
, end
);
1137 if (!(ASCII_a
<= c
&& c
<= ASCII_z
) && !(ASCII_A
<= c
&& c
<= ASCII_Z
)) {
1142 *encodingName
= val
;
1144 *encoding
= encodingFinder(enc
, val
, ptr
- enc
->minBytesPerChar
);
1145 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)) {
1152 if (!XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_standalone
)
1153 || isGeneralTextEntity
) {
1157 if (XmlNameMatchesAscii(enc
, val
, ptr
- enc
->minBytesPerChar
, KW_yes
)) {
1161 else if (XmlNameMatchesAscii(enc
, val
, ptr
- enc
->minBytesPerChar
, KW_no
)) {
1169 while (isSpace(toAscii(enc
, ptr
, end
)))
1170 ptr
+= enc
->minBytesPerChar
;
1179 checkCharRefNumber(int result
)
1181 switch (result
>> 8) {
1182 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1183 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1186 if (latin1_encoding
.type
[result
] == BT_NONXML
)
1190 if (result
== 0xFFFE || result
== 0xFFFF)
1198 XmlUtf8Encode(int c
, char *buf
)
1201 /* minN is minimum legal resulting value for N byte sequence */
1210 buf
[0] = (char)(c
| UTF8_cval1
);
1214 buf
[0] = (char)((c
>> 6) | UTF8_cval2
);
1215 buf
[1] = (char)((c
& 0x3f) | 0x80);
1219 buf
[0] = (char)((c
>> 12) | UTF8_cval3
);
1220 buf
[1] = (char)(((c
>> 6) & 0x3f) | 0x80);
1221 buf
[2] = (char)((c
& 0x3f) | 0x80);
1225 buf
[0] = (char)((c
>> 18) | UTF8_cval4
);
1226 buf
[1] = (char)(((c
>> 12) & 0x3f) | 0x80);
1227 buf
[2] = (char)(((c
>> 6) & 0x3f) | 0x80);
1228 buf
[3] = (char)((c
& 0x3f) | 0x80);
1235 XmlUtf16Encode(int charNum
, unsigned short *buf
)
1239 if (charNum
< 0x10000) {
1240 buf
[0] = (unsigned short)charNum
;
1243 if (charNum
< 0x110000) {
1245 buf
[0] = (unsigned short)((charNum
>> 10) + 0xD800);
1246 buf
[1] = (unsigned short)((charNum
& 0x3FF) + 0xDC00);
1252 struct unknown_encoding
{
1253 struct normal_encoding normal
;
1256 unsigned short utf16
[256];
1260 #define AS_UNKNOWN_ENCODING(enc) ((const struct unknown_encoding *) (enc))
1263 XmlSizeOfUnknownEncoding(void)
1265 return sizeof(struct unknown_encoding
);
1268 static int PTRFASTCALL
1269 unknown_isName(const ENCODING
*enc
, const char *p
)
1271 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1272 int c
= uenc
->convert(uenc
->userData
, p
);
1275 return UCS2_GET_NAMING(namePages
, c
>> 8, c
& 0xFF);
1278 static int PTRFASTCALL
1279 unknown_isNmstrt(const ENCODING
*enc
, const char *p
)
1281 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1282 int c
= uenc
->convert(uenc
->userData
, p
);
1285 return UCS2_GET_NAMING(nmstrtPages
, c
>> 8, c
& 0xFF);
1288 static int PTRFASTCALL
1289 unknown_isInvalid(const ENCODING
*enc
, const char *p
)
1291 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1292 int c
= uenc
->convert(uenc
->userData
, p
);
1293 return (c
& ~0xFFFF) || checkCharRefNumber(c
) < 0;
1297 unknown_toUtf8(const ENCODING
*enc
,
1298 const char **fromP
, const char *fromLim
,
1299 char **toP
, const char *toLim
)
1301 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1302 char buf
[XML_UTF8_ENCODE_MAX
];
1306 if (*fromP
== fromLim
)
1308 utf8
= uenc
->utf8
[(unsigned char)**fromP
];
1311 int c
= uenc
->convert(uenc
->userData
, *fromP
);
1312 n
= XmlUtf8Encode(c
, buf
);
1313 if (n
> toLim
- *toP
)
1316 *fromP
+= (AS_NORMAL_ENCODING(enc
)->type
[(unsigned char)**fromP
]
1320 if (n
> toLim
- *toP
)
1325 *(*toP
)++ = *utf8
++;
1331 unknown_toUtf16(const ENCODING
*enc
,
1332 const char **fromP
, const char *fromLim
,
1333 unsigned short **toP
, const unsigned short *toLim
)
1335 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1336 while (*fromP
!= fromLim
&& *toP
!= toLim
) {
1337 unsigned short c
= uenc
->utf16
[(unsigned char)**fromP
];
1339 c
= (unsigned short)
1340 uenc
->convert(uenc
->userData
, *fromP
);
1341 *fromP
+= (AS_NORMAL_ENCODING(enc
)->type
[(unsigned char)**fromP
]
1351 XmlInitUnknownEncoding(void *mem
,
1357 struct unknown_encoding
*e
= (struct unknown_encoding
*)mem
;
1358 for (i
= 0; i
< (int)sizeof(struct normal_encoding
); i
++)
1359 ((char *)mem
)[i
] = ((char *)&latin1_encoding
)[i
];
1360 for (i
= 0; i
< 128; i
++)
1361 if (latin1_encoding
.type
[i
] != BT_OTHER
1362 && latin1_encoding
.type
[i
] != BT_NONXML
1365 for (i
= 0; i
< 256; i
++) {
1368 e
->normal
.type
[i
] = BT_MALFORM
;
1369 /* This shouldn't really get used. */
1370 e
->utf16
[i
] = 0xFFFF;
1377 e
->normal
.type
[i
] = (unsigned char)(BT_LEAD2
- (c
+ 2));
1381 else if (c
< 0x80) {
1382 if (latin1_encoding
.type
[c
] != BT_OTHER
1383 && latin1_encoding
.type
[c
] != BT_NONXML
1386 e
->normal
.type
[i
] = latin1_encoding
.type
[c
];
1388 e
->utf8
[i
][1] = (char)c
;
1389 e
->utf16
[i
] = (unsigned short)(c
== 0 ? 0xFFFF : c
);
1391 else if (checkCharRefNumber(c
) < 0) {
1392 e
->normal
.type
[i
] = BT_NONXML
;
1393 /* This shouldn't really get used. */
1394 e
->utf16
[i
] = 0xFFFF;
1401 if (UCS2_GET_NAMING(nmstrtPages
, c
>> 8, c
& 0xff))
1402 e
->normal
.type
[i
] = BT_NMSTRT
;
1403 else if (UCS2_GET_NAMING(namePages
, c
>> 8, c
& 0xff))
1404 e
->normal
.type
[i
] = BT_NAME
;
1406 e
->normal
.type
[i
] = BT_OTHER
;
1407 e
->utf8
[i
][0] = (char)XmlUtf8Encode(c
, e
->utf8
[i
] + 1);
1408 e
->utf16
[i
] = (unsigned short)c
;
1411 e
->userData
= userData
;
1412 e
->convert
= convert
;
1414 e
->normal
.isName2
= unknown_isName
;
1415 e
->normal
.isName3
= unknown_isName
;
1416 e
->normal
.isName4
= unknown_isName
;
1417 e
->normal
.isNmstrt2
= unknown_isNmstrt
;
1418 e
->normal
.isNmstrt3
= unknown_isNmstrt
;
1419 e
->normal
.isNmstrt4
= unknown_isNmstrt
;
1420 e
->normal
.isInvalid2
= unknown_isInvalid
;
1421 e
->normal
.isInvalid3
= unknown_isInvalid
;
1422 e
->normal
.isInvalid4
= unknown_isInvalid
;
1424 e
->normal
.enc
.utf8Convert
= unknown_toUtf8
;
1425 e
->normal
.enc
.utf16Convert
= unknown_toUtf16
;
1426 return &(e
->normal
.enc
);
1429 /* If this enumeration is changed, getEncodingIndex and encodings
1430 must also be changed. */
1439 /* must match encodingNames up to here */
1443 static const char KW_ISO_8859_1
[] = {
1444 ASCII_I
, ASCII_S
, ASCII_O
, ASCII_MINUS
, ASCII_8
, ASCII_8
, ASCII_5
, ASCII_9
,
1445 ASCII_MINUS
, ASCII_1
, '\0'
1447 static const char KW_US_ASCII
[] = {
1448 ASCII_U
, ASCII_S
, ASCII_MINUS
, ASCII_A
, ASCII_S
, ASCII_C
, ASCII_I
, ASCII_I
,
1451 static const char KW_UTF_8
[] = {
1452 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_8
, '\0'
1454 static const char KW_UTF_16
[] = {
1455 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, '\0'
1457 static const char KW_UTF_16BE
[] = {
1458 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, ASCII_B
, ASCII_E
,
1461 static const char KW_UTF_16LE
[] = {
1462 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, ASCII_L
, ASCII_E
,
1467 getEncodingIndex(const char *name
)
1469 static const char * const encodingNames
[] = {
1480 for (i
= 0; i
< (int)(sizeof(encodingNames
)/sizeof(encodingNames
[0])); i
++)
1481 if (streqci(name
, encodingNames
[i
]))
1486 /* For binary compatibility, we store the index of the encoding
1487 specified at initialization in the isUtf16 member.
1490 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1491 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1493 /* This is what detects the encoding. encodingTable maps from
1494 encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
1495 the external (protocol) specified encoding; state is
1496 XML_CONTENT_STATE if we're parsing an external text entity, and
1497 XML_PROLOG_STATE otherwise.
1502 initScan(const ENCODING
* const *encodingTable
,
1503 const INIT_ENCODING
*enc
,
1507 const char **nextTokPtr
)
1509 const ENCODING
**encPtr
;
1512 return XML_TOK_NONE
;
1513 encPtr
= enc
->encPtr
;
1514 if (ptr
+ 1 == end
) {
1515 /* only a single byte available for auto-detection */
1516 #ifndef XML_DTD /* FIXME */
1517 /* a well-formed document entity must have more than one byte */
1518 if (state
!= XML_CONTENT_STATE
)
1519 return XML_TOK_PARTIAL
;
1521 /* so we're parsing an external text entity... */
1522 /* if UTF-16 was externally specified, then we need at least 2 bytes */
1523 switch (INIT_ENC_INDEX(enc
)) {
1527 return XML_TOK_PARTIAL
;
1529 switch ((unsigned char)*ptr
) {
1532 case 0xEF: /* possibly first byte of UTF-8 BOM */
1533 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1534 && state
== XML_CONTENT_STATE
)
1539 return XML_TOK_PARTIAL
;
1543 switch (((unsigned char)ptr
[0] << 8) | (unsigned char)ptr
[1]) {
1545 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1546 && state
== XML_CONTENT_STATE
)
1548 *nextTokPtr
= ptr
+ 2;
1549 *encPtr
= encodingTable
[UTF_16BE_ENC
];
1551 /* 00 3C is handled in the default case */
1553 if ((INIT_ENC_INDEX(enc
) == UTF_16BE_ENC
1554 || INIT_ENC_INDEX(enc
) == UTF_16_ENC
)
1555 && state
== XML_CONTENT_STATE
)
1557 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1558 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1560 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1561 && state
== XML_CONTENT_STATE
)
1563 *nextTokPtr
= ptr
+ 2;
1564 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1567 /* Maybe a UTF-8 BOM (EF BB BF) */
1568 /* If there's an explicitly specified (external) encoding
1569 of ISO-8859-1 or some flavour of UTF-16
1570 and this is an external text entity,
1571 don't look for the BOM,
1572 because it might be a legal data.
1574 if (state
== XML_CONTENT_STATE
) {
1575 int e
= INIT_ENC_INDEX(enc
);
1576 if (e
== ISO_8859_1_ENC
|| e
== UTF_16BE_ENC
1577 || e
== UTF_16LE_ENC
|| e
== UTF_16_ENC
)
1581 return XML_TOK_PARTIAL
;
1582 if ((unsigned char)ptr
[2] == 0xBF) {
1583 *nextTokPtr
= ptr
+ 3;
1584 *encPtr
= encodingTable
[UTF_8_ENC
];
1589 if (ptr
[0] == '\0') {
1590 /* 0 isn't a legal data character. Furthermore a document
1591 entity can only start with ASCII characters. So the only
1592 way this can fail to be big-endian UTF-16 if it it's an
1593 external parsed general entity that's labelled as
1596 if (state
== XML_CONTENT_STATE
&& INIT_ENC_INDEX(enc
) == UTF_16LE_ENC
)
1598 *encPtr
= encodingTable
[UTF_16BE_ENC
];
1599 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1601 else if (ptr
[1] == '\0') {
1602 /* We could recover here in the case:
1603 - parsing an external entity
1605 - no externally specified encoding
1606 - no encoding declaration
1607 by assuming UTF-16LE. But we don't, because this would mean when
1608 presented just with a single byte, we couldn't reliably determine
1609 whether we needed further bytes.
1611 if (state
== XML_CONTENT_STATE
)
1613 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1614 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1619 *encPtr
= encodingTable
[INIT_ENC_INDEX(enc
)];
1620 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1626 #include "xmltok_ns.c"
1632 #define NS(x) x ## NS
1633 #define ns(x) x ## _ns
1635 #include "xmltok_ns.c"
1641 XmlInitUnknownEncodingNS(void *mem
,
1646 ENCODING
*enc
= XmlInitUnknownEncoding(mem
, table
, convert
, userData
);
1648 ((struct normal_encoding
*)enc
)->type
[ASCII_COLON
] = BT_COLON
;
1654 /* BEGIN MOZILLA CHANGE (Mozilla extensions for QName checking) */
1655 #ifdef MOZILLA_CLIENT
1656 #include "moz_extensions.c"
1657 #endif /* MOZILLA_CLIENT */
1658 /* END MOZILLA CHANGE */