1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2 See the file COPYING for copying permission.
5 #ifdef COMPILED_FROM_DSP
9 #elif defined(MACOS_CLASSIC)
10 #include "macconfig.h"
12 #include "expat_config.h"
13 #endif /* ndef COMPILED_FROM_DSP */
20 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
22 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
26 { PREFIX(prologTok), PREFIX(contentTok), \
27 PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
28 { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
30 PREFIX(nameMatchesAscii), \
34 PREFIX(charRefNumber), \
35 PREFIX(predefinedEntityName), \
36 PREFIX(updatePosition), \
39 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
41 #define UCS2_GET_NAMING(pages, hi, lo) \
42 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
44 /* A 2 byte UTF-8 representation splits the characters 11 bits between
45 the bottom 5 and 6 bits of the bytes. We need 8 bits to index into
46 pages, 3 bits to add to that index and 5 bits to generate the mask.
48 #define UTF8_GET_NAMING2(pages, byte) \
49 (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
50 + ((((byte)[0]) & 3) << 1) \
51 + ((((byte)[1]) >> 5) & 1)] \
52 & (1 << (((byte)[1]) & 0x1F)))
54 /* A 3 byte UTF-8 representation splits the characters 16 bits between
55 the bottom 4, 6 and 6 bits of the bytes. We need 8 bits to index
56 into pages, 3 bits to add to that index and 5 bits to generate the
59 #define UTF8_GET_NAMING3(pages, byte) \
60 (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
61 + ((((byte)[1]) >> 2) & 0xF)] \
63 + ((((byte)[1]) & 3) << 1) \
64 + ((((byte)[2]) >> 5) & 1)] \
65 & (1 << (((byte)[2]) & 0x1F)))
67 #define UTF8_GET_NAMING(pages, p, n) \
69 ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
71 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
74 /* Detection of invalid UTF-8 sequences is based on Table 3.1B
75 of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
76 with the additional restriction of not allowing the Unicode
77 code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
78 Implementation details:
79 (A & 0x80) == 0 means A < 0x80
81 (A & 0xC0) == 0xC0 means A > 0xBF
84 #define UTF8_INVALID2(p) \
85 ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
87 #define UTF8_INVALID3(p) \
88 (((p)[2] & 0x80) == 0 \
90 ((*p) == 0xEF && (p)[1] == 0xBF \
94 ((p)[2] & 0xC0) == 0xC0) \
98 (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
100 ((p)[1] & 0x80) == 0 \
102 ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
104 #define UTF8_INVALID4(p) \
105 (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 \
107 ((p)[2] & 0x80) == 0 || ((p)[2] & 0xC0) == 0xC0 \
111 (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
113 ((p)[1] & 0x80) == 0 \
115 ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
117 static int PTRFASTCALL
118 isNever(const ENCODING
*enc
, const char *p
)
123 static int PTRFASTCALL
124 utf8_isName2(const ENCODING
*enc
, const char *p
)
126 return UTF8_GET_NAMING2(namePages
, (const unsigned char *)p
);
129 static int PTRFASTCALL
130 utf8_isName3(const ENCODING
*enc
, const char *p
)
132 return UTF8_GET_NAMING3(namePages
, (const unsigned char *)p
);
135 #define utf8_isName4 isNever
137 static int PTRFASTCALL
138 utf8_isNmstrt2(const ENCODING
*enc
, const char *p
)
140 return UTF8_GET_NAMING2(nmstrtPages
, (const unsigned char *)p
);
143 static int PTRFASTCALL
144 utf8_isNmstrt3(const ENCODING
*enc
, const char *p
)
146 return UTF8_GET_NAMING3(nmstrtPages
, (const unsigned char *)p
);
149 #define utf8_isNmstrt4 isNever
151 static int PTRFASTCALL
152 utf8_isInvalid2(const ENCODING
*enc
, const char *p
)
154 return UTF8_INVALID2((const unsigned char *)p
);
157 static int PTRFASTCALL
158 utf8_isInvalid3(const ENCODING
*enc
, const char *p
)
160 return UTF8_INVALID3((const unsigned char *)p
);
163 static int PTRFASTCALL
164 utf8_isInvalid4(const ENCODING
*enc
, const char *p
)
166 return UTF8_INVALID4((const unsigned char *)p
);
169 struct normal_encoding
{
171 unsigned char type
[256];
173 int (PTRFASTCALL
*byteType
)(const ENCODING
*, const char *);
174 int (PTRFASTCALL
*isNameMin
)(const ENCODING
*, const char *);
175 int (PTRFASTCALL
*isNmstrtMin
)(const ENCODING
*, const char *);
176 int (PTRFASTCALL
*byteToAscii
)(const ENCODING
*, const char *);
177 int (PTRCALL
*charMatches
)(const ENCODING
*, const char *, int);
178 #endif /* XML_MIN_SIZE */
179 int (PTRFASTCALL
*isName2
)(const ENCODING
*, const char *);
180 int (PTRFASTCALL
*isName3
)(const ENCODING
*, const char *);
181 int (PTRFASTCALL
*isName4
)(const ENCODING
*, const char *);
182 int (PTRFASTCALL
*isNmstrt2
)(const ENCODING
*, const char *);
183 int (PTRFASTCALL
*isNmstrt3
)(const ENCODING
*, const char *);
184 int (PTRFASTCALL
*isNmstrt4
)(const ENCODING
*, const char *);
185 int (PTRFASTCALL
*isInvalid2
)(const ENCODING
*, const char *);
186 int (PTRFASTCALL
*isInvalid3
)(const ENCODING
*, const char *);
187 int (PTRFASTCALL
*isInvalid4
)(const ENCODING
*, const char *);
190 #define AS_NORMAL_ENCODING(enc) ((const struct normal_encoding *) (enc))
194 #define STANDARD_VTABLE(E) \
203 #define STANDARD_VTABLE(E) /* as nothing */
207 #define NORMAL_VTABLE(E) \
218 static int FASTCALL
checkCharRefNumber(int);
220 #include "xmltok_impl.h"
224 #define sb_isNameMin isNever
225 #define sb_isNmstrtMin isNever
229 #define MINBPC(enc) ((enc)->minBytesPerChar)
231 /* minimum bytes per character */
232 #define MINBPC(enc) 1
235 #define SB_BYTE_TYPE(enc, p) \
236 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
239 static int PTRFASTCALL
240 sb_byteType(const ENCODING
*enc
, const char *p
)
242 return SB_BYTE_TYPE(enc
, p
);
244 #define BYTE_TYPE(enc, p) \
245 (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
247 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
251 #define BYTE_TO_ASCII(enc, p) \
252 (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
253 static int PTRFASTCALL
254 sb_byteToAscii(const ENCODING
*enc
, const char *p
)
259 #define BYTE_TO_ASCII(enc, p) (*(p))
262 #define IS_NAME_CHAR(enc, p, n) \
263 (AS_NORMAL_ENCODING(enc)->isName ## n(enc, p))
264 #define IS_NMSTRT_CHAR(enc, p, n) \
265 (AS_NORMAL_ENCODING(enc)->isNmstrt ## n(enc, p))
266 #define IS_INVALID_CHAR(enc, p, n) \
267 (AS_NORMAL_ENCODING(enc)->isInvalid ## n(enc, p))
270 #define IS_NAME_CHAR_MINBPC(enc, p) \
271 (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
272 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
273 (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
275 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
276 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
280 #define CHAR_MATCHES(enc, p, c) \
281 (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
283 sb_charMatches(const ENCODING
*enc
, const char *p
, int c
)
288 /* c is an ASCII character */
289 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
292 #define PREFIX(ident) normal_ ## ident
293 #include "xmltok_impl.c"
300 #undef IS_NAME_CHAR_MINBPC
301 #undef IS_NMSTRT_CHAR
302 #undef IS_NMSTRT_CHAR_MINBPC
303 #undef IS_INVALID_CHAR
305 enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
313 utf8_toUtf8(const ENCODING
*enc
,
314 const char **fromP
, const char *fromLim
,
315 char **toP
, const char *toLim
)
319 if (fromLim
- *fromP
> toLim
- *toP
) {
320 /* Avoid copying partial characters. */
321 for (fromLim
= *fromP
+ (toLim
- *toP
); fromLim
> *fromP
; fromLim
--)
322 if (((unsigned char)fromLim
[-1] & 0xc0) != 0x80)
325 for (to
= *toP
, from
= *fromP
; from
!= fromLim
; from
++, to
++)
332 utf8_toUtf16(const ENCODING
*enc
,
333 const char **fromP
, const char *fromLim
,
334 unsigned short **toP
, const unsigned short *toLim
)
336 unsigned short *to
= *toP
;
337 const char *from
= *fromP
;
338 while (from
!= fromLim
&& to
!= toLim
) {
339 switch (((struct normal_encoding
*)enc
)->type
[(unsigned char)*from
]) {
341 *to
++ = (unsigned short)(((from
[0] & 0x1f) << 6) | (from
[1] & 0x3f));
345 *to
++ = (unsigned short)(((from
[0] & 0xf) << 12)
346 | ((from
[1] & 0x3f) << 6) | (from
[2] & 0x3f));
354 n
= ((from
[0] & 0x7) << 18) | ((from
[1] & 0x3f) << 12)
355 | ((from
[2] & 0x3f) << 6) | (from
[3] & 0x3f);
357 to
[0] = (unsigned short)((n
>> 10) | 0xD800);
358 to
[1] = (unsigned short)((n
& 0x3FF) | 0xDC00);
374 static const struct normal_encoding utf8_encoding_ns
= {
375 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
377 #include "asciitab.h"
380 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
384 static const struct normal_encoding utf8_encoding
= {
385 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
387 #define BT_COLON BT_NMSTRT
388 #include "asciitab.h"
392 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
397 static const struct normal_encoding internal_utf8_encoding_ns
= {
398 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
400 #include "iasciitab.h"
403 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
408 static const struct normal_encoding internal_utf8_encoding
= {
409 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
411 #define BT_COLON BT_NMSTRT
412 #include "iasciitab.h"
416 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
420 latin1_toUtf8(const ENCODING
*enc
,
421 const char **fromP
, const char *fromLim
,
422 char **toP
, const char *toLim
)
426 if (*fromP
== fromLim
)
428 c
= (unsigned char)**fromP
;
430 if (toLim
- *toP
< 2)
432 *(*toP
)++ = (char)((c
>> 6) | UTF8_cval2
);
433 *(*toP
)++ = (char)((c
& 0x3f) | 0x80);
439 *(*toP
)++ = *(*fromP
)++;
445 latin1_toUtf16(const ENCODING
*enc
,
446 const char **fromP
, const char *fromLim
,
447 unsigned short **toP
, const unsigned short *toLim
)
449 while (*fromP
!= fromLim
&& *toP
!= toLim
)
450 *(*toP
)++ = (unsigned char)*(*fromP
)++;
455 static const struct normal_encoding latin1_encoding_ns
= {
456 { VTABLE1
, latin1_toUtf8
, latin1_toUtf16
, 1, 0, 0 },
458 #include "asciitab.h"
459 #include "latin1tab.h"
466 static const struct normal_encoding latin1_encoding
= {
467 { VTABLE1
, latin1_toUtf8
, latin1_toUtf16
, 1, 0, 0 },
469 #define BT_COLON BT_NMSTRT
470 #include "asciitab.h"
472 #include "latin1tab.h"
478 ascii_toUtf8(const ENCODING
*enc
,
479 const char **fromP
, const char *fromLim
,
480 char **toP
, const char *toLim
)
482 while (*fromP
!= fromLim
&& *toP
!= toLim
)
483 *(*toP
)++ = *(*fromP
)++;
488 static const struct normal_encoding ascii_encoding_ns
= {
489 { VTABLE1
, ascii_toUtf8
, latin1_toUtf16
, 1, 1, 0 },
491 #include "asciitab.h"
499 static const struct normal_encoding ascii_encoding
= {
500 { VTABLE1
, ascii_toUtf8
, latin1_toUtf16
, 1, 1, 0 },
502 #define BT_COLON BT_NMSTRT
503 #include "asciitab.h"
510 static int PTRFASTCALL
511 unicode_byte_type(char hi
, char lo
)
513 switch ((unsigned char)hi
) {
514 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
516 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
519 switch ((unsigned char)lo
) {
529 #define DEFINE_UTF16_TO_UTF8(E) \
530 static void PTRCALL \
531 E ## toUtf8(const ENCODING *enc, \
532 const char **fromP, const char *fromLim, \
533 char **toP, const char *toLim) \
536 for (from = *fromP; from != fromLim; from += 2) { \
539 unsigned char lo = GET_LO(from); \
540 unsigned char hi = GET_HI(from); \
544 if (*toP == toLim) { \
552 case 0x1: case 0x2: case 0x3: \
553 case 0x4: case 0x5: case 0x6: case 0x7: \
554 if (toLim - *toP < 2) { \
558 *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
559 *(*toP)++ = ((lo & 0x3f) | 0x80); \
562 if (toLim - *toP < 3) { \
566 /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
567 *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
568 *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
569 *(*toP)++ = ((lo & 0x3f) | 0x80); \
571 case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
572 if (toLim - *toP < 4) { \
576 plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
577 *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
578 *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
580 lo2 = GET_LO(from); \
581 *(*toP)++ = (((lo & 0x3) << 4) \
582 | ((GET_HI(from) & 0x3) << 2) \
585 *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
592 #define DEFINE_UTF16_TO_UTF16(E) \
593 static void PTRCALL \
594 E ## toUtf16(const ENCODING *enc, \
595 const char **fromP, const char *fromLim, \
596 unsigned short **toP, const unsigned short *toLim) \
598 /* Avoid copying first half only of surrogate */ \
599 if (fromLim - *fromP > ((toLim - *toP) << 1) \
600 && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
602 for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
603 *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
606 #define SET2(ptr, ch) \
607 (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
608 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
609 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
611 DEFINE_UTF16_TO_UTF8(little2_
)
612 DEFINE_UTF16_TO_UTF16(little2_
)
618 #define SET2(ptr, ch) \
619 (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
620 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
621 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
623 DEFINE_UTF16_TO_UTF8(big2_
)
624 DEFINE_UTF16_TO_UTF16(big2_
)
630 #define LITTLE2_BYTE_TYPE(enc, p) \
632 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
633 : unicode_byte_type((p)[1], (p)[0]))
634 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
635 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
636 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
637 UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
638 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
639 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
643 static int PTRFASTCALL
644 little2_byteType(const ENCODING
*enc
, const char *p
)
646 return LITTLE2_BYTE_TYPE(enc
, p
);
649 static int PTRFASTCALL
650 little2_byteToAscii(const ENCODING
*enc
, const char *p
)
652 return LITTLE2_BYTE_TO_ASCII(enc
, p
);
656 little2_charMatches(const ENCODING
*enc
, const char *p
, int c
)
658 return LITTLE2_CHAR_MATCHES(enc
, p
, c
);
661 static int PTRFASTCALL
662 little2_isNameMin(const ENCODING
*enc
, const char *p
)
664 return LITTLE2_IS_NAME_CHAR_MINBPC(enc
, p
);
667 static int PTRFASTCALL
668 little2_isNmstrtMin(const ENCODING
*enc
, const char *p
)
670 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc
, p
);
674 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
676 #else /* not XML_MIN_SIZE */
679 #define PREFIX(ident) little2_ ## ident
680 #define MINBPC(enc) 2
681 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
682 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
683 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
684 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
685 #define IS_NAME_CHAR(enc, p, n) 0
686 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
687 #define IS_NMSTRT_CHAR(enc, p, n) (0)
688 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
690 #include "xmltok_impl.c"
697 #undef IS_NAME_CHAR_MINBPC
698 #undef IS_NMSTRT_CHAR
699 #undef IS_NMSTRT_CHAR_MINBPC
700 #undef IS_INVALID_CHAR
702 #endif /* not XML_MIN_SIZE */
706 static const struct normal_encoding little2_encoding_ns
= {
708 #if BYTEORDER == 1234
715 #include "asciitab.h"
716 #include "latin1tab.h"
718 STANDARD_VTABLE(little2_
)
723 static const struct normal_encoding little2_encoding
= {
725 #if BYTEORDER == 1234
732 #define BT_COLON BT_NMSTRT
733 #include "asciitab.h"
735 #include "latin1tab.h"
737 STANDARD_VTABLE(little2_
)
740 #if BYTEORDER != 4321
744 static const struct normal_encoding internal_little2_encoding_ns
= {
747 #include "iasciitab.h"
748 #include "latin1tab.h"
750 STANDARD_VTABLE(little2_
)
755 static const struct normal_encoding internal_little2_encoding
= {
758 #define BT_COLON BT_NMSTRT
759 #include "iasciitab.h"
761 #include "latin1tab.h"
763 STANDARD_VTABLE(little2_
)
769 #define BIG2_BYTE_TYPE(enc, p) \
771 ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
772 : unicode_byte_type((p)[0], (p)[1]))
773 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
774 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
775 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
776 UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
777 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
778 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
782 static int PTRFASTCALL
783 big2_byteType(const ENCODING
*enc
, const char *p
)
785 return BIG2_BYTE_TYPE(enc
, p
);
788 static int PTRFASTCALL
789 big2_byteToAscii(const ENCODING
*enc
, const char *p
)
791 return BIG2_BYTE_TO_ASCII(enc
, p
);
795 big2_charMatches(const ENCODING
*enc
, const char *p
, int c
)
797 return BIG2_CHAR_MATCHES(enc
, p
, c
);
800 static int PTRFASTCALL
801 big2_isNameMin(const ENCODING
*enc
, const char *p
)
803 return BIG2_IS_NAME_CHAR_MINBPC(enc
, p
);
806 static int PTRFASTCALL
807 big2_isNmstrtMin(const ENCODING
*enc
, const char *p
)
809 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc
, p
);
813 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
815 #else /* not XML_MIN_SIZE */
818 #define PREFIX(ident) big2_ ## ident
819 #define MINBPC(enc) 2
820 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
821 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
822 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
823 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
824 #define IS_NAME_CHAR(enc, p, n) 0
825 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
826 #define IS_NMSTRT_CHAR(enc, p, n) (0)
827 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
829 #include "xmltok_impl.c"
836 #undef IS_NAME_CHAR_MINBPC
837 #undef IS_NMSTRT_CHAR
838 #undef IS_NMSTRT_CHAR_MINBPC
839 #undef IS_INVALID_CHAR
841 #endif /* not XML_MIN_SIZE */
845 static const struct normal_encoding big2_encoding_ns
= {
847 #if BYTEORDER == 4321
854 #include "asciitab.h"
855 #include "latin1tab.h"
857 STANDARD_VTABLE(big2_
)
862 static const struct normal_encoding big2_encoding
= {
864 #if BYTEORDER == 4321
871 #define BT_COLON BT_NMSTRT
872 #include "asciitab.h"
874 #include "latin1tab.h"
876 STANDARD_VTABLE(big2_
)
879 #if BYTEORDER != 1234
883 static const struct normal_encoding internal_big2_encoding_ns
= {
886 #include "iasciitab.h"
887 #include "latin1tab.h"
889 STANDARD_VTABLE(big2_
)
894 static const struct normal_encoding internal_big2_encoding
= {
897 #define BT_COLON BT_NMSTRT
898 #include "iasciitab.h"
900 #include "latin1tab.h"
902 STANDARD_VTABLE(big2_
)
910 streqci(const char *s1
, const char *s2
)
915 if (ASCII_a
<= c1
&& c1
<= ASCII_z
)
916 c1
+= ASCII_A
- ASCII_a
;
917 if (ASCII_a
<= c2
&& c2
<= ASCII_z
)
918 c2
+= ASCII_A
- ASCII_a
;
928 initUpdatePosition(const ENCODING
*enc
, const char *ptr
,
929 const char *end
, POSITION
*pos
)
931 normal_updatePosition(&utf8_encoding
.enc
, ptr
, end
, pos
);
935 toAscii(const ENCODING
*enc
, const char *ptr
, const char *end
)
939 XmlUtf8Convert(enc
, &ptr
, end
, &p
, p
+ 1);
959 /* Return 1 if there's just optional white space or there's an S
960 followed by name=val.
963 parsePseudoAttribute(const ENCODING
*enc
,
966 const char **namePtr
,
967 const char **nameEndPtr
,
969 const char **nextTokPtr
)
977 if (!isSpace(toAscii(enc
, ptr
, end
))) {
982 ptr
+= enc
->minBytesPerChar
;
983 } while (isSpace(toAscii(enc
, ptr
, end
)));
990 c
= toAscii(enc
, ptr
, end
);
995 if (c
== ASCII_EQUALS
) {
1002 ptr
+= enc
->minBytesPerChar
;
1003 } while (isSpace(c
= toAscii(enc
, ptr
, end
)));
1004 if (c
!= ASCII_EQUALS
) {
1010 ptr
+= enc
->minBytesPerChar
;
1012 if (ptr
== *namePtr
) {
1016 ptr
+= enc
->minBytesPerChar
;
1017 c
= toAscii(enc
, ptr
, end
);
1018 while (isSpace(c
)) {
1019 ptr
+= enc
->minBytesPerChar
;
1020 c
= toAscii(enc
, ptr
, end
);
1022 if (c
!= ASCII_QUOT
&& c
!= ASCII_APOS
) {
1027 ptr
+= enc
->minBytesPerChar
;
1029 for (;; ptr
+= enc
->minBytesPerChar
) {
1030 c
= toAscii(enc
, ptr
, end
);
1033 if (!(ASCII_a
<= c
&& c
<= ASCII_z
)
1034 && !(ASCII_A
<= c
&& c
<= ASCII_Z
)
1035 && !(ASCII_0
<= c
&& c
<= ASCII_9
)
1036 && c
!= ASCII_PERIOD
1038 && c
!= ASCII_UNDERSCORE
) {
1043 *nextTokPtr
= ptr
+ enc
->minBytesPerChar
;
1047 static const char KW_version
[] = {
1048 ASCII_v
, ASCII_e
, ASCII_r
, ASCII_s
, ASCII_i
, ASCII_o
, ASCII_n
, '\0'
1051 static const char KW_encoding
[] = {
1052 ASCII_e
, ASCII_n
, ASCII_c
, ASCII_o
, ASCII_d
, ASCII_i
, ASCII_n
, ASCII_g
, '\0'
1055 static const char KW_standalone
[] = {
1056 ASCII_s
, ASCII_t
, ASCII_a
, ASCII_n
, ASCII_d
, ASCII_a
, ASCII_l
, ASCII_o
,
1057 ASCII_n
, ASCII_e
, '\0'
1060 static const char KW_yes
[] = {
1061 ASCII_y
, ASCII_e
, ASCII_s
, '\0'
1064 static const char KW_no
[] = {
1065 ASCII_n
, ASCII_o
, '\0'
1069 doParseXmlDecl(const ENCODING
*(*encodingFinder
)(const ENCODING
*,
1072 int isGeneralTextEntity
,
1073 const ENCODING
*enc
,
1076 const char **badPtr
,
1077 const char **versionPtr
,
1078 const char **versionEndPtr
,
1079 const char **encodingName
,
1080 const ENCODING
**encoding
,
1083 const char *val
= NULL
;
1084 const char *name
= NULL
;
1085 const char *nameEnd
= NULL
;
1086 ptr
+= 5 * enc
->minBytesPerChar
;
1087 end
-= 2 * enc
->minBytesPerChar
;
1088 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)
1093 if (!XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_version
)) {
1094 if (!isGeneralTextEntity
) {
1103 *versionEndPtr
= ptr
;
1104 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)) {
1109 if (isGeneralTextEntity
) {
1110 /* a TextDecl must have an EncodingDecl */
1117 if (XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_encoding
)) {
1118 int c
= toAscii(enc
, val
, end
);
1119 if (!(ASCII_a
<= c
&& c
<= ASCII_z
) && !(ASCII_A
<= c
&& c
<= ASCII_Z
)) {
1124 *encodingName
= val
;
1126 *encoding
= encodingFinder(enc
, val
, ptr
- enc
->minBytesPerChar
);
1127 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)) {
1134 if (!XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_standalone
)
1135 || isGeneralTextEntity
) {
1139 if (XmlNameMatchesAscii(enc
, val
, ptr
- enc
->minBytesPerChar
, KW_yes
)) {
1143 else if (XmlNameMatchesAscii(enc
, val
, ptr
- enc
->minBytesPerChar
, KW_no
)) {
1151 while (isSpace(toAscii(enc
, ptr
, end
)))
1152 ptr
+= enc
->minBytesPerChar
;
1161 checkCharRefNumber(int result
)
1163 switch (result
>> 8) {
1164 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1165 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1168 if (latin1_encoding
.type
[result
] == BT_NONXML
)
1172 if (result
== 0xFFFE || result
== 0xFFFF)
1180 XmlUtf8Encode(int c
, char *buf
)
1183 /* minN is minimum legal resulting value for N byte sequence */
1192 buf
[0] = (char)(c
| UTF8_cval1
);
1196 buf
[0] = (char)((c
>> 6) | UTF8_cval2
);
1197 buf
[1] = (char)((c
& 0x3f) | 0x80);
1201 buf
[0] = (char)((c
>> 12) | UTF8_cval3
);
1202 buf
[1] = (char)(((c
>> 6) & 0x3f) | 0x80);
1203 buf
[2] = (char)((c
& 0x3f) | 0x80);
1207 buf
[0] = (char)((c
>> 18) | UTF8_cval4
);
1208 buf
[1] = (char)(((c
>> 12) & 0x3f) | 0x80);
1209 buf
[2] = (char)(((c
>> 6) & 0x3f) | 0x80);
1210 buf
[3] = (char)((c
& 0x3f) | 0x80);
1217 XmlUtf16Encode(int charNum
, unsigned short *buf
)
1221 if (charNum
< 0x10000) {
1222 buf
[0] = (unsigned short)charNum
;
1225 if (charNum
< 0x110000) {
1227 buf
[0] = (unsigned short)((charNum
>> 10) + 0xD800);
1228 buf
[1] = (unsigned short)((charNum
& 0x3FF) + 0xDC00);
1234 struct unknown_encoding
{
1235 struct normal_encoding normal
;
1236 int (*convert
)(void *userData
, const char *p
);
1238 unsigned short utf16
[256];
1242 #define AS_UNKNOWN_ENCODING(enc) ((const struct unknown_encoding *) (enc))
1245 XmlSizeOfUnknownEncoding(void)
1247 return sizeof(struct unknown_encoding
);
1250 static int PTRFASTCALL
1251 unknown_isName(const ENCODING
*enc
, const char *p
)
1253 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1254 int c
= uenc
->convert(uenc
->userData
, p
);
1257 return UCS2_GET_NAMING(namePages
, c
>> 8, c
& 0xFF);
1260 static int PTRFASTCALL
1261 unknown_isNmstrt(const ENCODING
*enc
, const char *p
)
1263 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1264 int c
= uenc
->convert(uenc
->userData
, p
);
1267 return UCS2_GET_NAMING(nmstrtPages
, c
>> 8, c
& 0xFF);
1270 static int PTRFASTCALL
1271 unknown_isInvalid(const ENCODING
*enc
, const char *p
)
1273 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1274 int c
= uenc
->convert(uenc
->userData
, p
);
1275 return (c
& ~0xFFFF) || checkCharRefNumber(c
) < 0;
1279 unknown_toUtf8(const ENCODING
*enc
,
1280 const char **fromP
, const char *fromLim
,
1281 char **toP
, const char *toLim
)
1283 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1284 char buf
[XML_UTF8_ENCODE_MAX
];
1288 if (*fromP
== fromLim
)
1290 utf8
= uenc
->utf8
[(unsigned char)**fromP
];
1293 int c
= uenc
->convert(uenc
->userData
, *fromP
);
1294 n
= XmlUtf8Encode(c
, buf
);
1295 if (n
> toLim
- *toP
)
1298 *fromP
+= (AS_NORMAL_ENCODING(enc
)->type
[(unsigned char)**fromP
]
1302 if (n
> toLim
- *toP
)
1307 *(*toP
)++ = *utf8
++;
1313 unknown_toUtf16(const ENCODING
*enc
,
1314 const char **fromP
, const char *fromLim
,
1315 unsigned short **toP
, const unsigned short *toLim
)
1317 const struct unknown_encoding
*uenc
= AS_UNKNOWN_ENCODING(enc
);
1318 while (*fromP
!= fromLim
&& *toP
!= toLim
) {
1319 unsigned short c
= uenc
->utf16
[(unsigned char)**fromP
];
1321 c
= (unsigned short)
1322 uenc
->convert(uenc
->userData
, *fromP
);
1323 *fromP
+= (AS_NORMAL_ENCODING(enc
)->type
[(unsigned char)**fromP
]
1333 XmlInitUnknownEncoding(void *mem
,
1339 struct unknown_encoding
*e
= (struct unknown_encoding
*)mem
;
1340 for (i
= 0; i
< (int)sizeof(struct normal_encoding
); i
++)
1341 ((char *)mem
)[i
] = ((char *)&latin1_encoding
)[i
];
1342 for (i
= 0; i
< 128; i
++)
1343 if (latin1_encoding
.type
[i
] != BT_OTHER
1344 && latin1_encoding
.type
[i
] != BT_NONXML
1347 for (i
= 0; i
< 256; i
++) {
1350 e
->normal
.type
[i
] = BT_MALFORM
;
1351 /* This shouldn't really get used. */
1352 e
->utf16
[i
] = 0xFFFF;
1359 e
->normal
.type
[i
] = (unsigned char)(BT_LEAD2
- (c
+ 2));
1363 else if (c
< 0x80) {
1364 if (latin1_encoding
.type
[c
] != BT_OTHER
1365 && latin1_encoding
.type
[c
] != BT_NONXML
1368 e
->normal
.type
[i
] = latin1_encoding
.type
[c
];
1370 e
->utf8
[i
][1] = (char)c
;
1371 e
->utf16
[i
] = (unsigned short)(c
== 0 ? 0xFFFF : c
);
1373 else if (checkCharRefNumber(c
) < 0) {
1374 e
->normal
.type
[i
] = BT_NONXML
;
1375 /* This shouldn't really get used. */
1376 e
->utf16
[i
] = 0xFFFF;
1383 if (UCS2_GET_NAMING(nmstrtPages
, c
>> 8, c
& 0xff))
1384 e
->normal
.type
[i
] = BT_NMSTRT
;
1385 else if (UCS2_GET_NAMING(namePages
, c
>> 8, c
& 0xff))
1386 e
->normal
.type
[i
] = BT_NAME
;
1388 e
->normal
.type
[i
] = BT_OTHER
;
1389 e
->utf8
[i
][0] = (char)XmlUtf8Encode(c
, e
->utf8
[i
] + 1);
1390 e
->utf16
[i
] = (unsigned short)c
;
1393 e
->userData
= userData
;
1394 e
->convert
= convert
;
1396 e
->normal
.isName2
= unknown_isName
;
1397 e
->normal
.isName3
= unknown_isName
;
1398 e
->normal
.isName4
= unknown_isName
;
1399 e
->normal
.isNmstrt2
= unknown_isNmstrt
;
1400 e
->normal
.isNmstrt3
= unknown_isNmstrt
;
1401 e
->normal
.isNmstrt4
= unknown_isNmstrt
;
1402 e
->normal
.isInvalid2
= unknown_isInvalid
;
1403 e
->normal
.isInvalid3
= unknown_isInvalid
;
1404 e
->normal
.isInvalid4
= unknown_isInvalid
;
1406 e
->normal
.enc
.utf8Convert
= unknown_toUtf8
;
1407 e
->normal
.enc
.utf16Convert
= unknown_toUtf16
;
1408 return &(e
->normal
.enc
);
1411 /* If this enumeration is changed, getEncodingIndex and encodings
1412 must also be changed. */
1421 /* must match encodingNames up to here */
1425 static const char KW_ISO_8859_1
[] = {
1426 ASCII_I
, ASCII_S
, ASCII_O
, ASCII_MINUS
, ASCII_8
, ASCII_8
, ASCII_5
, ASCII_9
,
1427 ASCII_MINUS
, ASCII_1
, '\0'
1429 static const char KW_US_ASCII
[] = {
1430 ASCII_U
, ASCII_S
, ASCII_MINUS
, ASCII_A
, ASCII_S
, ASCII_C
, ASCII_I
, ASCII_I
,
1433 static const char KW_UTF_8
[] = {
1434 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_8
, '\0'
1436 static const char KW_UTF_16
[] = {
1437 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, '\0'
1439 static const char KW_UTF_16BE
[] = {
1440 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, ASCII_B
, ASCII_E
,
1443 static const char KW_UTF_16LE
[] = {
1444 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, ASCII_L
, ASCII_E
,
1449 getEncodingIndex(const char *name
)
1451 static const char *encodingNames
[] = {
1462 for (i
= 0; i
< (int)(sizeof(encodingNames
)/sizeof(encodingNames
[0])); i
++)
1463 if (streqci(name
, encodingNames
[i
]))
1468 /* For binary compatibility, we store the index of the encoding
1469 specified at initialization in the isUtf16 member.
1472 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1473 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1475 /* This is what detects the encoding. encodingTable maps from
1476 encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
1477 the external (protocol) specified encoding; state is
1478 XML_CONTENT_STATE if we're parsing an external text entity, and
1479 XML_PROLOG_STATE otherwise.
1484 initScan(const ENCODING
**encodingTable
,
1485 const INIT_ENCODING
*enc
,
1489 const char **nextTokPtr
)
1491 const ENCODING
**encPtr
;
1494 return XML_TOK_NONE
;
1495 encPtr
= enc
->encPtr
;
1496 if (ptr
+ 1 == end
) {
1497 /* only a single byte available for auto-detection */
1498 #ifndef XML_DTD /* FIXME */
1499 /* a well-formed document entity must have more than one byte */
1500 if (state
!= XML_CONTENT_STATE
)
1501 return XML_TOK_PARTIAL
;
1503 /* so we're parsing an external text entity... */
1504 /* if UTF-16 was externally specified, then we need at least 2 bytes */
1505 switch (INIT_ENC_INDEX(enc
)) {
1509 return XML_TOK_PARTIAL
;
1511 switch ((unsigned char)*ptr
) {
1514 case 0xEF: /* possibly first byte of UTF-8 BOM */
1515 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1516 && state
== XML_CONTENT_STATE
)
1521 return XML_TOK_PARTIAL
;
1525 switch (((unsigned char)ptr
[0] << 8) | (unsigned char)ptr
[1]) {
1527 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1528 && state
== XML_CONTENT_STATE
)
1530 *nextTokPtr
= ptr
+ 2;
1531 *encPtr
= encodingTable
[UTF_16BE_ENC
];
1533 /* 00 3C is handled in the default case */
1535 if ((INIT_ENC_INDEX(enc
) == UTF_16BE_ENC
1536 || INIT_ENC_INDEX(enc
) == UTF_16_ENC
)
1537 && state
== XML_CONTENT_STATE
)
1539 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1540 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1542 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1543 && state
== XML_CONTENT_STATE
)
1545 *nextTokPtr
= ptr
+ 2;
1546 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1549 /* Maybe a UTF-8 BOM (EF BB BF) */
1550 /* If there's an explicitly specified (external) encoding
1551 of ISO-8859-1 or some flavour of UTF-16
1552 and this is an external text entity,
1553 don't look for the BOM,
1554 because it might be a legal data.
1556 if (state
== XML_CONTENT_STATE
) {
1557 int e
= INIT_ENC_INDEX(enc
);
1558 if (e
== ISO_8859_1_ENC
|| e
== UTF_16BE_ENC
1559 || e
== UTF_16LE_ENC
|| e
== UTF_16_ENC
)
1563 return XML_TOK_PARTIAL
;
1564 if ((unsigned char)ptr
[2] == 0xBF) {
1565 *nextTokPtr
= ptr
+ 3;
1566 *encPtr
= encodingTable
[UTF_8_ENC
];
1571 if (ptr
[0] == '\0') {
1572 /* 0 isn't a legal data character. Furthermore a document
1573 entity can only start with ASCII characters. So the only
1574 way this can fail to be big-endian UTF-16 if it it's an
1575 external parsed general entity that's labelled as
1578 if (state
== XML_CONTENT_STATE
&& INIT_ENC_INDEX(enc
) == UTF_16LE_ENC
)
1580 *encPtr
= encodingTable
[UTF_16BE_ENC
];
1581 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1583 else if (ptr
[1] == '\0') {
1584 /* We could recover here in the case:
1585 - parsing an external entity
1587 - no externally specified encoding
1588 - no encoding declaration
1589 by assuming UTF-16LE. But we don't, because this would mean when
1590 presented just with a single byte, we couldn't reliably determine
1591 whether we needed further bytes.
1593 if (state
== XML_CONTENT_STATE
)
1595 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1596 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1601 *encPtr
= encodingTable
[INIT_ENC_INDEX(enc
)];
1602 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1608 #include "xmltok_ns.c"
1614 #define NS(x) x ## NS
1615 #define ns(x) x ## _ns
1617 #include "xmltok_ns.c"
1623 XmlInitUnknownEncodingNS(void *mem
,
1628 ENCODING
*enc
= XmlInitUnknownEncoding(mem
, table
, convert
, userData
);
1630 ((struct normal_encoding
*)enc
)->type
[ASCII_COLON
] = BT_COLON
;