2 Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
3 See the file copying.txt for copying permission.
11 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
13 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
17 { PREFIX(prologTok), PREFIX(contentTok), \
18 PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
19 { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
21 PREFIX(nameMatchesAscii), \
25 PREFIX(charRefNumber), \
26 PREFIX(predefinedEntityName), \
27 PREFIX(updatePosition), \
30 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
32 #define UCS2_GET_NAMING(pages, hi, lo) \
33 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
35 /* A 2 byte UTF-8 representation splits the characters 11 bits
36 between the bottom 5 and 6 bits of the bytes.
37 We need 8 bits to index into pages, 3 bits to add to that index and
38 5 bits to generate the mask. */
39 #define UTF8_GET_NAMING2(pages, byte) \
40 (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
41 + ((((byte)[0]) & 3) << 1) \
42 + ((((byte)[1]) >> 5) & 1)] \
43 & (1 << (((byte)[1]) & 0x1F)))
45 /* A 3 byte UTF-8 representation splits the characters 16 bits
46 between the bottom 4, 6 and 6 bits of the bytes.
47 We need 8 bits to index into pages, 3 bits to add to that index and
48 5 bits to generate the mask. */
49 #define UTF8_GET_NAMING3(pages, byte) \
50 (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
51 + ((((byte)[1]) >> 2) & 0xF)] \
53 + ((((byte)[1]) & 3) << 1) \
54 + ((((byte)[2]) >> 5) & 1)] \
55 & (1 << (((byte)[2]) & 0x1F)))
57 #define UTF8_GET_NAMING(pages, p, n) \
59 ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
61 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
64 #define UTF8_INVALID3(p) \
66 ? (((p)[1] & 0x20) != 0) \
68 ? ((p)[1] == 0xBF && ((p)[2] == 0xBF || (p)[2] == 0xBE)) \
71 #define UTF8_INVALID4(p) ((*p) == 0xF4 && ((p)[1] & 0x30) != 0)
74 int isNever(const ENCODING
*enc
, const char *p
)
80 int utf8_isName2(const ENCODING
*enc
, const char *p
)
82 return UTF8_GET_NAMING2(namePages
, (const unsigned char *)p
);
86 int utf8_isName3(const ENCODING
*enc
, const char *p
)
88 return UTF8_GET_NAMING3(namePages
, (const unsigned char *)p
);
91 #define utf8_isName4 isNever
94 int utf8_isNmstrt2(const ENCODING
*enc
, const char *p
)
96 return UTF8_GET_NAMING2(nmstrtPages
, (const unsigned char *)p
);
100 int utf8_isNmstrt3(const ENCODING
*enc
, const char *p
)
102 return UTF8_GET_NAMING3(nmstrtPages
, (const unsigned char *)p
);
105 #define utf8_isNmstrt4 isNever
107 #define utf8_isInvalid2 isNever
110 int utf8_isInvalid3(const ENCODING
*enc
, const char *p
)
112 return UTF8_INVALID3((const unsigned char *)p
);
116 int utf8_isInvalid4(const ENCODING
*enc
, const char *p
)
118 return UTF8_INVALID4((const unsigned char *)p
);
121 struct normal_encoding
{
123 unsigned char type
[256];
125 int (*byteType
)(const ENCODING
*, const char *);
126 int (*isNameMin
)(const ENCODING
*, const char *);
127 int (*isNmstrtMin
)(const ENCODING
*, const char *);
128 int (*byteToAscii
)(const ENCODING
*, const char *);
129 int (*charMatches
)(const ENCODING
*, const char *, int);
130 #endif /* XML_MIN_SIZE */
131 int (*isName2
)(const ENCODING
*, const char *);
132 int (*isName3
)(const ENCODING
*, const char *);
133 int (*isName4
)(const ENCODING
*, const char *);
134 int (*isNmstrt2
)(const ENCODING
*, const char *);
135 int (*isNmstrt3
)(const ENCODING
*, const char *);
136 int (*isNmstrt4
)(const ENCODING
*, const char *);
137 int (*isInvalid2
)(const ENCODING
*, const char *);
138 int (*isInvalid3
)(const ENCODING
*, const char *);
139 int (*isInvalid4
)(const ENCODING
*, const char *);
144 #define STANDARD_VTABLE(E) \
153 #define STANDARD_VTABLE(E) /* as nothing */
157 #define NORMAL_VTABLE(E) \
168 static int checkCharRefNumber(int);
170 #include "xmltok_impl.h"
174 #define sb_isNameMin isNever
175 #define sb_isNmstrtMin isNever
179 #define MINBPC(enc) ((enc)->minBytesPerChar)
181 /* minimum bytes per character */
182 #define MINBPC(enc) 1
185 #define SB_BYTE_TYPE(enc, p) \
186 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
190 int sb_byteType(const ENCODING
*enc
, const char *p
)
192 return SB_BYTE_TYPE(enc
, p
);
194 #define BYTE_TYPE(enc, p) \
195 (((const struct normal_encoding *)(enc))->byteType(enc, p))
197 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
201 #define BYTE_TO_ASCII(enc, p) \
202 (((const struct normal_encoding *)(enc))->byteToAscii(enc, p))
204 int sb_byteToAscii(const ENCODING
*enc
, const char *p
)
209 #define BYTE_TO_ASCII(enc, p) (*(p))
212 #define IS_NAME_CHAR(enc, p, n) \
213 (((const struct normal_encoding *)(enc))->isName ## n(enc, p))
214 #define IS_NMSTRT_CHAR(enc, p, n) \
215 (((const struct normal_encoding *)(enc))->isNmstrt ## n(enc, p))
216 #define IS_INVALID_CHAR(enc, p, n) \
217 (((const struct normal_encoding *)(enc))->isInvalid ## n(enc, p))
220 #define IS_NAME_CHAR_MINBPC(enc, p) \
221 (((const struct normal_encoding *)(enc))->isNameMin(enc, p))
222 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
223 (((const struct normal_encoding *)(enc))->isNmstrtMin(enc, p))
225 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
226 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
230 #define CHAR_MATCHES(enc, p, c) \
231 (((const struct normal_encoding *)(enc))->charMatches(enc, p, c))
233 int sb_charMatches(const ENCODING
*enc
, const char *p
, int c
)
238 /* c is an ASCII character */
239 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
242 #define PREFIX(ident) normal_ ## ident
243 #include "xmltok_impl.c"
250 #undef IS_NAME_CHAR_MINBPC
251 #undef IS_NMSTRT_CHAR
252 #undef IS_NMSTRT_CHAR_MINBPC
253 #undef IS_INVALID_CHAR
255 enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
263 void utf8_toUtf8(const ENCODING
*enc
,
264 const char **fromP
, const char *fromLim
,
265 char **toP
, const char *toLim
)
269 if (fromLim
- *fromP
> toLim
- *toP
) {
270 /* Avoid copying partial characters. */
271 for (fromLim
= *fromP
+ (toLim
- *toP
); fromLim
> *fromP
; fromLim
--)
272 if (((unsigned char)fromLim
[-1] & 0xc0) != 0x80)
275 for (to
= *toP
, from
= *fromP
; from
!= fromLim
; from
++, to
++)
282 void utf8_toUtf16(const ENCODING
*enc
,
283 const char **fromP
, const char *fromLim
,
284 unsigned short **toP
, const unsigned short *toLim
)
286 unsigned short *to
= *toP
;
287 const char *from
= *fromP
;
288 while (from
!= fromLim
&& to
!= toLim
) {
289 switch (((struct normal_encoding
*)enc
)->type
[(unsigned char)*from
]) {
291 *to
++ = ((from
[0] & 0x1f) << 6) | (from
[1] & 0x3f);
295 *to
++ = ((from
[0] & 0xf) << 12) | ((from
[1] & 0x3f) << 6) | (from
[2] & 0x3f);
303 n
= ((from
[0] & 0x7) << 18) | ((from
[1] & 0x3f) << 12) | ((from
[2] & 0x3f) << 6) | (from
[3] & 0x3f);
305 to
[0] = (unsigned short)((n
>> 10) | 0xD800);
306 to
[1] = (unsigned short)((n
& 0x3FF) | 0xDC00);
321 static const struct normal_encoding utf8_encoding_ns
= {
322 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
324 #include "asciitab.h"
327 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
331 static const struct normal_encoding utf8_encoding
= {
332 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
334 #define BT_COLON BT_NMSTRT
335 #include "asciitab.h"
339 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
344 static const struct normal_encoding internal_utf8_encoding_ns
= {
345 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
347 #include "iasciitab.h"
350 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
355 static const struct normal_encoding internal_utf8_encoding
= {
356 { VTABLE1
, utf8_toUtf8
, utf8_toUtf16
, 1, 1, 0 },
358 #define BT_COLON BT_NMSTRT
359 #include "iasciitab.h"
363 STANDARD_VTABLE(sb_
) NORMAL_VTABLE(utf8_
)
367 void latin1_toUtf8(const ENCODING
*enc
,
368 const char **fromP
, const char *fromLim
,
369 char **toP
, const char *toLim
)
373 if (*fromP
== fromLim
)
375 c
= (unsigned char)**fromP
;
377 if (toLim
- *toP
< 2)
379 *(*toP
)++ = ((c
>> 6) | UTF8_cval2
);
380 *(*toP
)++ = ((c
& 0x3f) | 0x80);
386 *(*toP
)++ = *(*fromP
)++;
392 void latin1_toUtf16(const ENCODING
*enc
,
393 const char **fromP
, const char *fromLim
,
394 unsigned short **toP
, const unsigned short *toLim
)
396 while (*fromP
!= fromLim
&& *toP
!= toLim
)
397 *(*toP
)++ = (unsigned char)*(*fromP
)++;
402 static const struct normal_encoding latin1_encoding_ns
= {
403 { VTABLE1
, latin1_toUtf8
, latin1_toUtf16
, 1, 0, 0 },
405 #include "asciitab.h"
406 #include "latin1tab.h"
413 static const struct normal_encoding latin1_encoding
= {
414 { VTABLE1
, latin1_toUtf8
, latin1_toUtf16
, 1, 0, 0 },
416 #define BT_COLON BT_NMSTRT
417 #include "asciitab.h"
419 #include "latin1tab.h"
425 void ascii_toUtf8(const ENCODING
*enc
,
426 const char **fromP
, const char *fromLim
,
427 char **toP
, const char *toLim
)
429 while (*fromP
!= fromLim
&& *toP
!= toLim
)
430 *(*toP
)++ = *(*fromP
)++;
435 static const struct normal_encoding ascii_encoding_ns
= {
436 { VTABLE1
, ascii_toUtf8
, latin1_toUtf16
, 1, 1, 0 },
438 #include "asciitab.h"
446 static const struct normal_encoding ascii_encoding
= {
447 { VTABLE1
, ascii_toUtf8
, latin1_toUtf16
, 1, 1, 0 },
449 #define BT_COLON BT_NMSTRT
450 #include "asciitab.h"
457 static int unicode_byte_type(char hi
, char lo
)
459 switch ((unsigned char)hi
) {
460 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
462 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
465 switch ((unsigned char)lo
) {
475 #define DEFINE_UTF16_TO_UTF8(E) \
477 void E ## toUtf8(const ENCODING *enc, \
478 const char **fromP, const char *fromLim, \
479 char **toP, const char *toLim) \
482 for (from = *fromP; from != fromLim; from += 2) { \
485 unsigned char lo = GET_LO(from); \
486 unsigned char hi = GET_HI(from); \
490 if (*toP == toLim) { \
498 case 0x1: case 0x2: case 0x3: \
499 case 0x4: case 0x5: case 0x6: case 0x7: \
500 if (toLim - *toP < 2) { \
504 *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
505 *(*toP)++ = ((lo & 0x3f) | 0x80); \
508 if (toLim - *toP < 3) { \
512 /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
513 *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
514 *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
515 *(*toP)++ = ((lo & 0x3f) | 0x80); \
517 case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
518 if (toLim - *toP < 4) { \
522 plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
523 *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
524 *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
526 lo2 = GET_LO(from); \
527 *(*toP)++ = (((lo & 0x3) << 4) \
528 | ((GET_HI(from) & 0x3) << 2) \
531 *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
538 #define DEFINE_UTF16_TO_UTF16(E) \
540 void E ## toUtf16(const ENCODING *enc, \
541 const char **fromP, const char *fromLim, \
542 unsigned short **toP, const unsigned short *toLim) \
544 /* Avoid copying first half only of surrogate */ \
545 if (fromLim - *fromP > ((toLim - *toP) << 1) \
546 && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
548 for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
549 *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
552 #define SET2(ptr, ch) \
553 (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
554 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
555 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
557 DEFINE_UTF16_TO_UTF8(little2_
)
558 DEFINE_UTF16_TO_UTF16(little2_
)
564 #define SET2(ptr, ch) \
565 (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
566 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
567 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
569 DEFINE_UTF16_TO_UTF8(big2_
)
570 DEFINE_UTF16_TO_UTF16(big2_
)
576 #define LITTLE2_BYTE_TYPE(enc, p) \
578 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
579 : unicode_byte_type((p)[1], (p)[0]))
580 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
581 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
582 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
583 UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
584 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
585 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
590 int little2_byteType(const ENCODING
*enc
, const char *p
)
592 return LITTLE2_BYTE_TYPE(enc
, p
);
596 int little2_byteToAscii(const ENCODING
*enc
, const char *p
)
598 return LITTLE2_BYTE_TO_ASCII(enc
, p
);
602 int little2_charMatches(const ENCODING
*enc
, const char *p
, int c
)
604 return LITTLE2_CHAR_MATCHES(enc
, p
, c
);
608 int little2_isNameMin(const ENCODING
*enc
, const char *p
)
610 return LITTLE2_IS_NAME_CHAR_MINBPC(enc
, p
);
614 int little2_isNmstrtMin(const ENCODING
*enc
, const char *p
)
616 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc
, p
);
620 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
622 #else /* not XML_MIN_SIZE */
625 #define PREFIX(ident) little2_ ## ident
626 #define MINBPC(enc) 2
627 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
628 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
629 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
630 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
631 #define IS_NAME_CHAR(enc, p, n) 0
632 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
633 #define IS_NMSTRT_CHAR(enc, p, n) (0)
634 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
636 #include "xmltok_impl.c"
643 #undef IS_NAME_CHAR_MINBPC
644 #undef IS_NMSTRT_CHAR
645 #undef IS_NMSTRT_CHAR_MINBPC
646 #undef IS_INVALID_CHAR
648 #endif /* not XML_MIN_SIZE */
652 static const struct normal_encoding little2_encoding_ns
= {
654 #if XML_BYTE_ORDER == 12
661 #include "asciitab.h"
662 #include "latin1tab.h"
664 STANDARD_VTABLE(little2_
)
669 static const struct normal_encoding little2_encoding
= {
671 #if XML_BYTE_ORDER == 12
678 #define BT_COLON BT_NMSTRT
679 #include "asciitab.h"
681 #include "latin1tab.h"
683 STANDARD_VTABLE(little2_
)
686 #if XML_BYTE_ORDER != 21
690 static const struct normal_encoding internal_little2_encoding_ns
= {
693 #include "iasciitab.h"
694 #include "latin1tab.h"
696 STANDARD_VTABLE(little2_
)
701 static const struct normal_encoding internal_little2_encoding
= {
704 #define BT_COLON BT_NMSTRT
705 #include "iasciitab.h"
707 #include "latin1tab.h"
709 STANDARD_VTABLE(little2_
)
715 #define BIG2_BYTE_TYPE(enc, p) \
717 ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
718 : unicode_byte_type((p)[0], (p)[1]))
719 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
720 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
721 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
722 UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
723 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
724 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
729 int big2_byteType(const ENCODING
*enc
, const char *p
)
731 return BIG2_BYTE_TYPE(enc
, p
);
735 int big2_byteToAscii(const ENCODING
*enc
, const char *p
)
737 return BIG2_BYTE_TO_ASCII(enc
, p
);
741 int big2_charMatches(const ENCODING
*enc
, const char *p
, int c
)
743 return BIG2_CHAR_MATCHES(enc
, p
, c
);
747 int big2_isNameMin(const ENCODING
*enc
, const char *p
)
749 return BIG2_IS_NAME_CHAR_MINBPC(enc
, p
);
753 int big2_isNmstrtMin(const ENCODING
*enc
, const char *p
)
755 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc
, p
);
759 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
761 #else /* not XML_MIN_SIZE */
764 #define PREFIX(ident) big2_ ## ident
765 #define MINBPC(enc) 2
766 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
767 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
768 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
769 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
770 #define IS_NAME_CHAR(enc, p, n) 0
771 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
772 #define IS_NMSTRT_CHAR(enc, p, n) (0)
773 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
775 #include "xmltok_impl.c"
782 #undef IS_NAME_CHAR_MINBPC
783 #undef IS_NMSTRT_CHAR
784 #undef IS_NMSTRT_CHAR_MINBPC
785 #undef IS_INVALID_CHAR
787 #endif /* not XML_MIN_SIZE */
791 static const struct normal_encoding big2_encoding_ns
= {
793 #if XML_BYTE_ORDER == 21
800 #include "asciitab.h"
801 #include "latin1tab.h"
803 STANDARD_VTABLE(big2_
)
808 static const struct normal_encoding big2_encoding
= {
810 #if XML_BYTE_ORDER == 21
817 #define BT_COLON BT_NMSTRT
818 #include "asciitab.h"
820 #include "latin1tab.h"
822 STANDARD_VTABLE(big2_
)
825 #if XML_BYTE_ORDER != 12
829 static const struct normal_encoding internal_big2_encoding_ns
= {
832 #include "iasciitab.h"
833 #include "latin1tab.h"
835 STANDARD_VTABLE(big2_
)
840 static const struct normal_encoding internal_big2_encoding
= {
843 #define BT_COLON BT_NMSTRT
844 #include "iasciitab.h"
846 #include "latin1tab.h"
848 STANDARD_VTABLE(big2_
)
856 int streqci(const char *s1
, const char *s2
)
861 if (ASCII_a
<= c1
&& c1
<= ASCII_z
)
862 c1
+= ASCII_A
- ASCII_a
;
863 if (ASCII_a
<= c2
&& c2
<= ASCII_z
)
864 c2
+= ASCII_A
- ASCII_a
;
874 void initUpdatePosition(const ENCODING
*enc
, const char *ptr
,
875 const char *end
, POSITION
*pos
)
877 normal_updatePosition(&utf8_encoding
.enc
, ptr
, end
, pos
);
881 int toAscii(const ENCODING
*enc
, const char *ptr
, const char *end
)
885 XmlUtf8Convert(enc
, &ptr
, end
, &p
, p
+ 1);
905 /* Return 1 if there's just optional white space
906 or there's an S followed by name=val. */
908 int parsePseudoAttribute(const ENCODING
*enc
,
911 const char **namePtr
,
912 const char **nameEndPtr
,
914 const char **nextTokPtr
)
922 if (!isSpace(toAscii(enc
, ptr
, end
))) {
927 ptr
+= enc
->minBytesPerChar
;
928 } while (isSpace(toAscii(enc
, ptr
, end
)));
935 c
= toAscii(enc
, ptr
, end
);
940 if (c
== ASCII_EQUALS
) {
947 ptr
+= enc
->minBytesPerChar
;
948 } while (isSpace(c
= toAscii(enc
, ptr
, end
)));
949 if (c
!= ASCII_EQUALS
) {
955 ptr
+= enc
->minBytesPerChar
;
957 if (ptr
== *namePtr
) {
961 ptr
+= enc
->minBytesPerChar
;
962 c
= toAscii(enc
, ptr
, end
);
964 ptr
+= enc
->minBytesPerChar
;
965 c
= toAscii(enc
, ptr
, end
);
967 if (c
!= ASCII_QUOT
&& c
!= ASCII_APOS
) {
972 ptr
+= enc
->minBytesPerChar
;
974 for (;; ptr
+= enc
->minBytesPerChar
) {
975 c
= toAscii(enc
, ptr
, end
);
978 if (!(ASCII_a
<= c
&& c
<= ASCII_z
)
979 && !(ASCII_A
<= c
&& c
<= ASCII_Z
)
980 && !(ASCII_0
<= c
&& c
<= ASCII_9
)
983 && c
!= ASCII_UNDERSCORE
) {
988 *nextTokPtr
= ptr
+ enc
->minBytesPerChar
;
992 static const char KW_version
[] = {
993 ASCII_v
, ASCII_e
, ASCII_r
, ASCII_s
, ASCII_i
, ASCII_o
, ASCII_n
, '\0'
996 static const char KW_encoding
[] = {
997 ASCII_e
, ASCII_n
, ASCII_c
, ASCII_o
, ASCII_d
, ASCII_i
, ASCII_n
, ASCII_g
, '\0'
1000 static const char KW_standalone
[] = {
1001 ASCII_s
, ASCII_t
, ASCII_a
, ASCII_n
, ASCII_d
, ASCII_a
, ASCII_l
, ASCII_o
, ASCII_n
, ASCII_e
, '\0'
1004 static const char KW_yes
[] = {
1005 ASCII_y
, ASCII_e
, ASCII_s
, '\0'
1008 static const char KW_no
[] = {
1009 ASCII_n
, ASCII_o
, '\0'
1013 int doParseXmlDecl(const ENCODING
*(*encodingFinder
)(const ENCODING
*,
1016 int isGeneralTextEntity
,
1017 const ENCODING
*enc
,
1020 const char **badPtr
,
1021 const char **versionPtr
,
1022 const char **encodingName
,
1023 const ENCODING
**encoding
,
1026 const char *val
= 0;
1027 const char *name
= 0;
1028 const char *nameEnd
= 0;
1029 ptr
+= 5 * enc
->minBytesPerChar
;
1030 end
-= 2 * enc
->minBytesPerChar
;
1031 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
) || !name
) {
1035 if (!XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_version
)) {
1036 if (!isGeneralTextEntity
) {
1044 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)) {
1049 if (isGeneralTextEntity
) {
1050 /* a TextDecl must have an EncodingDecl */
1057 if (XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_encoding
)) {
1058 int c
= toAscii(enc
, val
, end
);
1059 if (!(ASCII_a
<= c
&& c
<= ASCII_z
) && !(ASCII_A
<= c
&& c
<= ASCII_Z
)) {
1064 *encodingName
= val
;
1066 *encoding
= encodingFinder(enc
, val
, ptr
- enc
->minBytesPerChar
);
1067 if (!parsePseudoAttribute(enc
, ptr
, end
, &name
, &nameEnd
, &val
, &ptr
)) {
1074 if (!XmlNameMatchesAscii(enc
, name
, nameEnd
, KW_standalone
) || isGeneralTextEntity
) {
1078 if (XmlNameMatchesAscii(enc
, val
, ptr
- enc
->minBytesPerChar
, KW_yes
)) {
1082 else if (XmlNameMatchesAscii(enc
, val
, ptr
- enc
->minBytesPerChar
, KW_no
)) {
1090 while (isSpace(toAscii(enc
, ptr
, end
)))
1091 ptr
+= enc
->minBytesPerChar
;
1100 int checkCharRefNumber(int result
)
1102 switch (result
>> 8) {
1103 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1104 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1107 if (latin1_encoding
.type
[result
] == BT_NONXML
)
1111 if (result
== 0xFFFE || result
== 0xFFFF)
1118 int XmlUtf8Encode(int c
, char *buf
)
1121 /* minN is minimum legal resulting value for N byte sequence */
1130 buf
[0] = (c
| UTF8_cval1
);
1134 buf
[0] = ((c
>> 6) | UTF8_cval2
);
1135 buf
[1] = ((c
& 0x3f) | 0x80);
1139 buf
[0] = ((c
>> 12) | UTF8_cval3
);
1140 buf
[1] = (((c
>> 6) & 0x3f) | 0x80);
1141 buf
[2] = ((c
& 0x3f) | 0x80);
1145 buf
[0] = ((c
>> 18) | UTF8_cval4
);
1146 buf
[1] = (((c
>> 12) & 0x3f) | 0x80);
1147 buf
[2] = (((c
>> 6) & 0x3f) | 0x80);
1148 buf
[3] = ((c
& 0x3f) | 0x80);
1154 int XmlUtf16Encode(int charNum
, unsigned short *buf
)
1158 if (charNum
< 0x10000) {
1162 if (charNum
< 0x110000) {
1164 buf
[0] = (charNum
>> 10) + 0xD800;
1165 buf
[1] = (charNum
& 0x3FF) + 0xDC00;
1171 struct unknown_encoding
{
1172 struct normal_encoding normal
;
1173 int (*convert
)(void *userData
, const char *p
);
1175 unsigned short utf16
[256];
1179 int XmlSizeOfUnknownEncoding(void)
1181 return sizeof(struct unknown_encoding
);
1185 int unknown_isName(const ENCODING
*enc
, const char *p
)
1187 int c
= ((const struct unknown_encoding
*)enc
)
1188 ->convert(((const struct unknown_encoding
*)enc
)->userData
, p
);
1191 return UCS2_GET_NAMING(namePages
, c
>> 8, c
& 0xFF);
1195 int unknown_isNmstrt(const ENCODING
*enc
, const char *p
)
1197 int c
= ((const struct unknown_encoding
*)enc
)
1198 ->convert(((const struct unknown_encoding
*)enc
)->userData
, p
);
1201 return UCS2_GET_NAMING(nmstrtPages
, c
>> 8, c
& 0xFF);
1205 int unknown_isInvalid(const ENCODING
*enc
, const char *p
)
1207 int c
= ((const struct unknown_encoding
*)enc
)
1208 ->convert(((const struct unknown_encoding
*)enc
)->userData
, p
);
1209 return (c
& ~0xFFFF) || checkCharRefNumber(c
) < 0;
1213 void unknown_toUtf8(const ENCODING
*enc
,
1214 const char **fromP
, const char *fromLim
,
1215 char **toP
, const char *toLim
)
1217 char buf
[XML_UTF8_ENCODE_MAX
];
1221 if (*fromP
== fromLim
)
1223 utf8
= ((const struct unknown_encoding
*)enc
)->utf8
[(unsigned char)**fromP
];
1226 int c
= ((const struct unknown_encoding
*)enc
)
1227 ->convert(((const struct unknown_encoding
*)enc
)->userData
, *fromP
);
1228 n
= XmlUtf8Encode(c
, buf
);
1229 if (n
> toLim
- *toP
)
1232 *fromP
+= ((const struct normal_encoding
*)enc
)->type
[(unsigned char)**fromP
]
1236 if (n
> toLim
- *toP
)
1241 *(*toP
)++ = *utf8
++;
1247 void unknown_toUtf16(const ENCODING
*enc
,
1248 const char **fromP
, const char *fromLim
,
1249 unsigned short **toP
, const unsigned short *toLim
)
1251 while (*fromP
!= fromLim
&& *toP
!= toLim
) {
1253 = ((const struct unknown_encoding
*)enc
)->utf16
[(unsigned char)**fromP
];
1255 c
= (unsigned short)((const struct unknown_encoding
*)enc
)
1256 ->convert(((const struct unknown_encoding
*)enc
)->userData
, *fromP
);
1257 *fromP
+= ((const struct normal_encoding
*)enc
)->type
[(unsigned char)**fromP
]
1267 XmlInitUnknownEncoding(void *mem
,
1269 int (*convert
)(void *userData
, const char *p
),
1273 struct unknown_encoding
*e
= mem
;
1274 for (i
= 0; i
< (int)sizeof(struct normal_encoding
); i
++)
1275 ((char *)mem
)[i
] = ((char *)&latin1_encoding
)[i
];
1276 for (i
= 0; i
< 128; i
++)
1277 if (latin1_encoding
.type
[i
] != BT_OTHER
1278 && latin1_encoding
.type
[i
] != BT_NONXML
1281 for (i
= 0; i
< 256; i
++) {
1284 e
->normal
.type
[i
] = BT_MALFORM
;
1285 /* This shouldn't really get used. */
1286 e
->utf16
[i
] = 0xFFFF;
1293 e
->normal
.type
[i
] = BT_LEAD2
- (c
+ 2);
1297 else if (c
< 0x80) {
1298 if (latin1_encoding
.type
[c
] != BT_OTHER
1299 && latin1_encoding
.type
[c
] != BT_NONXML
1302 e
->normal
.type
[i
] = latin1_encoding
.type
[c
];
1304 e
->utf8
[i
][1] = (char)c
;
1305 e
->utf16
[i
] = c
== 0 ? 0xFFFF : c
;
1307 else if (checkCharRefNumber(c
) < 0) {
1308 e
->normal
.type
[i
] = BT_NONXML
;
1309 /* This shouldn't really get used. */
1310 e
->utf16
[i
] = 0xFFFF;
1317 if (UCS2_GET_NAMING(nmstrtPages
, c
>> 8, c
& 0xff))
1318 e
->normal
.type
[i
] = BT_NMSTRT
;
1319 else if (UCS2_GET_NAMING(namePages
, c
>> 8, c
& 0xff))
1320 e
->normal
.type
[i
] = BT_NAME
;
1322 e
->normal
.type
[i
] = BT_OTHER
;
1323 e
->utf8
[i
][0] = (char)XmlUtf8Encode(c
, e
->utf8
[i
] + 1);
1327 e
->userData
= userData
;
1328 e
->convert
= convert
;
1330 e
->normal
.isName2
= unknown_isName
;
1331 e
->normal
.isName3
= unknown_isName
;
1332 e
->normal
.isName4
= unknown_isName
;
1333 e
->normal
.isNmstrt2
= unknown_isNmstrt
;
1334 e
->normal
.isNmstrt3
= unknown_isNmstrt
;
1335 e
->normal
.isNmstrt4
= unknown_isNmstrt
;
1336 e
->normal
.isInvalid2
= unknown_isInvalid
;
1337 e
->normal
.isInvalid3
= unknown_isInvalid
;
1338 e
->normal
.isInvalid4
= unknown_isInvalid
;
1340 e
->normal
.enc
.utf8Convert
= unknown_toUtf8
;
1341 e
->normal
.enc
.utf16Convert
= unknown_toUtf16
;
1342 return &(e
->normal
.enc
);
1345 /* If this enumeration is changed, getEncodingIndex and encodings
1346 must also be changed. */
1355 /* must match encodingNames up to here */
1359 static const char KW_ISO_8859_1
[] = {
1360 ASCII_I
, ASCII_S
, ASCII_O
, ASCII_MINUS
, ASCII_8
, ASCII_8
, ASCII_5
, ASCII_9
, ASCII_MINUS
, ASCII_1
, '\0'
1362 static const char KW_US_ASCII
[] = {
1363 ASCII_U
, ASCII_S
, ASCII_MINUS
, ASCII_A
, ASCII_S
, ASCII_C
, ASCII_I
, ASCII_I
, '\0'
1365 static const char KW_UTF_8
[] = {
1366 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_8
, '\0'
1368 static const char KW_UTF_16
[] = {
1369 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, '\0'
1371 static const char KW_UTF_16BE
[] = {
1372 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, ASCII_B
, ASCII_E
, '\0'
1374 static const char KW_UTF_16LE
[] = {
1375 ASCII_U
, ASCII_T
, ASCII_F
, ASCII_MINUS
, ASCII_1
, ASCII_6
, ASCII_L
, ASCII_E
, '\0'
1379 int getEncodingIndex(const char *name
)
1381 static const char *encodingNames
[] = {
1392 for (i
= 0; i
< (int)(sizeof(encodingNames
)/sizeof(encodingNames
[0])); i
++)
1393 if (streqci(name
, encodingNames
[i
]))
1398 /* For binary compatibility, we store the index of the encoding specified
1399 at initialization in the isUtf16 member. */
1401 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1402 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1404 /* This is what detects the encoding.
1405 encodingTable maps from encoding indices to encodings;
1406 INIT_ENC_INDEX(enc) is the index of the external (protocol) specified encoding;
1407 state is XML_CONTENT_STATE if we're parsing an external text entity,
1408 and XML_PROLOG_STATE otherwise.
1413 int initScan(const ENCODING
**encodingTable
,
1414 const INIT_ENCODING
*enc
,
1418 const char **nextTokPtr
)
1420 const ENCODING
**encPtr
;
1423 return XML_TOK_NONE
;
1424 encPtr
= enc
->encPtr
;
1425 if (ptr
+ 1 == end
) {
1426 /* only a single byte available for auto-detection */
1427 #ifndef XML_DTD /* FIXME */
1428 /* a well-formed document entity must have more than one byte */
1429 if (state
!= XML_CONTENT_STATE
)
1430 return XML_TOK_PARTIAL
;
1432 /* so we're parsing an external text entity... */
1433 /* if UTF-16 was externally specified, then we need at least 2 bytes */
1434 switch (INIT_ENC_INDEX(enc
)) {
1438 return XML_TOK_PARTIAL
;
1440 switch ((unsigned char)*ptr
) {
1443 case 0xEF: /* possibly first byte of UTF-8 BOM */
1444 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1445 && state
== XML_CONTENT_STATE
)
1450 return XML_TOK_PARTIAL
;
1454 switch (((unsigned char)ptr
[0] << 8) | (unsigned char)ptr
[1]) {
1456 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1457 && state
== XML_CONTENT_STATE
)
1459 *nextTokPtr
= ptr
+ 2;
1460 *encPtr
= encodingTable
[UTF_16BE_ENC
];
1462 /* 00 3C is handled in the default case */
1464 if ((INIT_ENC_INDEX(enc
) == UTF_16BE_ENC
1465 || INIT_ENC_INDEX(enc
) == UTF_16_ENC
)
1466 && state
== XML_CONTENT_STATE
)
1468 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1469 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1471 if (INIT_ENC_INDEX(enc
) == ISO_8859_1_ENC
1472 && state
== XML_CONTENT_STATE
)
1474 *nextTokPtr
= ptr
+ 2;
1475 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1478 /* Maybe a UTF-8 BOM (EF BB BF) */
1479 /* If there's an explicitly specified (external) encoding
1480 of ISO-8859-1 or some flavour of UTF-16
1481 and this is an external text entity,
1482 don't look for the BOM,
1483 because it might be a legal data. */
1484 if (state
== XML_CONTENT_STATE
) {
1485 int e
= INIT_ENC_INDEX(enc
);
1486 if (e
== ISO_8859_1_ENC
|| e
== UTF_16BE_ENC
|| e
== UTF_16LE_ENC
|| e
== UTF_16_ENC
)
1490 return XML_TOK_PARTIAL
;
1491 if ((unsigned char)ptr
[2] == 0xBF) {
1492 *encPtr
= encodingTable
[UTF_8_ENC
];
1497 if (ptr
[0] == '\0') {
1498 /* 0 isn't a legal data character. Furthermore a document entity can only
1499 start with ASCII characters. So the only way this can fail to be big-endian
1500 UTF-16 if it it's an external parsed general entity that's labelled as
1502 if (state
== XML_CONTENT_STATE
&& INIT_ENC_INDEX(enc
) == UTF_16LE_ENC
)
1504 *encPtr
= encodingTable
[UTF_16BE_ENC
];
1505 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1507 else if (ptr
[1] == '\0') {
1508 /* We could recover here in the case:
1509 - parsing an external entity
1511 - no externally specified encoding
1512 - no encoding declaration
1513 by assuming UTF-16LE. But we don't, because this would mean when
1514 presented just with a single byte, we couldn't reliably determine
1515 whether we needed further bytes. */
1516 if (state
== XML_CONTENT_STATE
)
1518 *encPtr
= encodingTable
[UTF_16LE_ENC
];
1519 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1524 *encPtr
= encodingTable
[INIT_ENC_INDEX(enc
)];
1525 return XmlTok(*encPtr
, state
, ptr
, end
, nextTokPtr
);
1531 #include "xmltok_ns.c"
1537 #define NS(x) x ## NS
1538 #define ns(x) x ## _ns
1540 #include "xmltok_ns.c"
1546 XmlInitUnknownEncodingNS(void *mem
,
1548 int (*convert
)(void *userData
, const char *p
),
1551 ENCODING
*enc
= XmlInitUnknownEncoding(mem
, table
, convert
, userData
);
1553 ((struct normal_encoding
*)enc
)->type
[ASCII_COLON
] = BT_COLON
;