2 Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
3 See the file copying.txt for copying permission.
6 #include "xmlrpc_config.h"
12 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
14 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
18 { PREFIX(prologTok), PREFIX(contentTok), \
19 PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
20 { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
22 PREFIX(nameMatchesAscii), \
26 PREFIX(charRefNumber), \
27 PREFIX(predefinedEntityName), \
28 PREFIX(updatePosition), \
31 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
33 #define UCS2_GET_NAMING(pages, hi, lo) \
34 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
36 /* A 2 byte UTF-8 representation splits the characters 11 bits
37 between the bottom 5 and 6 bits of the bytes.
38 We need 8 bits to index into pages, 3 bits to add to that index and
39 5 bits to generate the mask. */
40 #define UTF8_GET_NAMING2(pages, byte) \
41 (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
42 + ((((byte)[0]) & 3) << 1) \
43 + ((((byte)[1]) >> 5) & 1)] \
44 & (1 << (((byte)[1]) & 0x1F)))
46 /* A 3 byte UTF-8 representation splits the characters 16 bits
47 between the bottom 4, 6 and 6 bits of the bytes.
48 We need 8 bits to index into pages, 3 bits to add to that index and
49 5 bits to generate the mask. */
50 #define UTF8_GET_NAMING3(pages, byte) \
51 (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
52 + ((((byte)[1]) >> 2) & 0xF)] \
54 + ((((byte)[1]) & 3) << 1) \
55 + ((((byte)[2]) >> 5) & 1)] \
56 & (1 << (((byte)[2]) & 0x1F)))
58 #define UTF8_GET_NAMING(pages, p, n) \
60 ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
62 ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
65 #define UTF8_INVALID3(p) \
67 ? (((p)[1] & 0x20) != 0) \
69 ? ((p)[1] == 0xBF && ((p)[2] == 0xBF || (p)[2] == 0xBE)) \
72 #define UTF8_INVALID4(p) ((*p) == 0xF4 && ((p)[1] & 0x30) != 0)
75 int isNever(const ENCODING *enc ATTR_UNUSED, const char *p ATTR_UNUSED)
81 int utf8_isName2(const ENCODING *enc ATTR_UNUSED, const char *p)
83 return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
87 int utf8_isName3(const ENCODING *enc ATTR_UNUSED, const char *p)
89 return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
92 #define utf8_isName4 isNever
95 int utf8_isNmstrt2(const ENCODING *enc ATTR_UNUSED, const char *p)
97 return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
101 int utf8_isNmstrt3(const ENCODING *enc ATTR_UNUSED, const char *p)
103 return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
106 #define utf8_isNmstrt4 isNever
108 #define utf8_isInvalid2 isNever
111 int utf8_isInvalid3(const ENCODING *enc ATTR_UNUSED, const char *p)
113 return UTF8_INVALID3((const unsigned char *)p);
117 int utf8_isInvalid4(const ENCODING *enc ATTR_UNUSED, const char *p)
119 return UTF8_INVALID4((const unsigned char *)p);
122 struct normal_encoding {
124 unsigned char type[256];
126 int (*byteType)(const ENCODING *, const char *);
127 int (*isNameMin)(const ENCODING *, const char *);
128 int (*isNmstrtMin)(const ENCODING *, const char *);
129 int (*byteToAscii)(const ENCODING *, const char *);
130 int (*charMatches)(const ENCODING *, const char *, int);
131 #endif /* XML_MIN_SIZE */
132 int (*isName2)(const ENCODING *, const char *);
133 int (*isName3)(const ENCODING *, const char *);
134 int (*isName4)(const ENCODING *, const char *);
135 int (*isNmstrt2)(const ENCODING *, const char *);
136 int (*isNmstrt3)(const ENCODING *, const char *);
137 int (*isNmstrt4)(const ENCODING *, const char *);
138 int (*isInvalid2)(const ENCODING *, const char *);
139 int (*isInvalid3)(const ENCODING *, const char *);
140 int (*isInvalid4)(const ENCODING *, const char *);
145 #define STANDARD_VTABLE(E) \
154 #define STANDARD_VTABLE(E) /* as nothing */
158 #define NORMAL_VTABLE(E) \
169 #define NULL_NORMAL_VTABLE \
170 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
172 static int checkCharRefNumber(int);
174 #include "xmltok_impl.h"
178 #define sb_isNameMin isNever
179 #define sb_isNmstrtMin isNever
183 #define MINBPC(enc) ((enc)->minBytesPerChar)
185 /* minimum bytes per character */
186 #define MINBPC(enc) 1
189 #define SB_BYTE_TYPE(enc, p) \
190 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
194 int sb_byteType(const ENCODING *enc, const char *p)
196 return SB_BYTE_TYPE(enc, p);
198 #define BYTE_TYPE(enc, p) \
199 (((const struct normal_encoding *)(enc))->byteType(enc, p))
201 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
205 #define BYTE_TO_ASCII(enc, p) \
206 (((const struct normal_encoding *)(enc))->byteToAscii(enc, p))
208 int sb_byteToAscii(const ENCODING *enc, const char *p)
213 #define BYTE_TO_ASCII(enc, p) (*(p))
216 #define IS_NAME_CHAR(enc, p, n) \
217 (((const struct normal_encoding *)(enc))->isName ## n(enc, p))
218 #define IS_NMSTRT_CHAR(enc, p, n) \
219 (((const struct normal_encoding *)(enc))->isNmstrt ## n(enc, p))
220 #define IS_INVALID_CHAR(enc, p, n) \
221 (((const struct normal_encoding *)(enc))->isInvalid ## n(enc, p))
224 #define IS_NAME_CHAR_MINBPC(enc, p) \
225 (((const struct normal_encoding *)(enc))->isNameMin(enc, p))
226 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
227 (((const struct normal_encoding *)(enc))->isNmstrtMin(enc, p))
229 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
230 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
234 #define CHAR_MATCHES(enc, p, c) \
235 (((const struct normal_encoding *)(enc))->charMatches(enc, p, c))
237 int sb_charMatches(const ENCODING *enc, const char *p, int c)
242 /* c is an ASCII character */
243 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
246 #define PREFIX(ident) normal_ ## ident
247 #include "xmltok_impl.c"
254 #undef IS_NAME_CHAR_MINBPC
255 #undef IS_NMSTRT_CHAR
256 #undef IS_NMSTRT_CHAR_MINBPC
257 #undef IS_INVALID_CHAR
259 enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
267 void utf8_toUtf8(const ENCODING * enc ATTR_UNUSED,
268 const char **fromP, const char *fromLim,
269 char **toP, const char *toLim)
273 if (fromLim - *fromP > toLim - *toP) {
274 /* Avoid copying partial characters. */
275 for (fromLim = *fromP + (toLim - *toP); fromLim > *fromP; fromLim--)
276 if (((unsigned char)fromLim[-1] & 0xc0) != 0x80)
279 for (to = *toP, from = *fromP; from != fromLim; from++, to++)
286 void utf8_toUtf16(const ENCODING *enc,
287 const char **fromP, const char *fromLim,
288 unsigned short **toP, const unsigned short *toLim)
290 unsigned short *to = *toP;
291 const char *from = *fromP;
292 while (from != fromLim && to != toLim) {
293 switch (((struct normal_encoding *)enc)->type[(unsigned char)*from]) {
295 *to++ = ((from[0] & 0x1f) << 6) | (from[1] & 0x3f);
299 *to++ = ((from[0] & 0xf) << 12) | ((from[1] & 0x3f) << 6) | (from[2] & 0x3f);
307 n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12) | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
309 to[0] = (unsigned short)((n >> 10) | 0xD800);
310 to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
325 static const struct normal_encoding utf8_encoding_ns = {
326 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
328 #include "asciitab.h"
331 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
335 static const struct normal_encoding utf8_encoding = {
336 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
338 #define BT_COLON BT_NMSTRT
339 #include "asciitab.h"
343 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
348 static const struct normal_encoding internal_utf8_encoding_ns = {
349 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
351 #include "iasciitab.h"
354 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
359 static const struct normal_encoding internal_utf8_encoding = {
360 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
362 #define BT_COLON BT_NMSTRT
363 #include "iasciitab.h"
367 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
371 void latin1_toUtf8(const ENCODING *enc ATTR_UNUSED,
372 const char **fromP, const char *fromLim,
373 char **toP, const char *toLim)
377 if (*fromP == fromLim)
379 c = (unsigned char)**fromP;
381 if (toLim - *toP < 2)
383 *(*toP)++ = ((c >> 6) | UTF8_cval2);
384 *(*toP)++ = ((c & 0x3f) | 0x80);
390 *(*toP)++ = *(*fromP)++;
396 void latin1_toUtf16(const ENCODING *enc ATTR_UNUSED,
397 const char **fromP, const char *fromLim,
398 unsigned short **toP, const unsigned short *toLim)
400 while (*fromP != fromLim && *toP != toLim)
401 *(*toP)++ = (unsigned char)*(*fromP)++;
406 static const struct normal_encoding latin1_encoding_ns = {
407 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
409 #include "asciitab.h"
410 #include "latin1tab.h"
412 STANDARD_VTABLE(sb_) NULL_NORMAL_VTABLE
417 static const struct normal_encoding latin1_encoding = {
418 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
420 #define BT_COLON BT_NMSTRT
421 #include "asciitab.h"
423 #include "latin1tab.h"
425 STANDARD_VTABLE(sb_) NULL_NORMAL_VTABLE
429 void ascii_toUtf8(const ENCODING *enc ATTR_UNUSED,
430 const char **fromP, const char *fromLim,
431 char **toP, const char *toLim)
433 while (*fromP != fromLim && *toP != toLim)
434 *(*toP)++ = *(*fromP)++;
439 static const struct normal_encoding ascii_encoding_ns = {
440 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
442 #include "asciitab.h"
450 static const struct normal_encoding ascii_encoding = {
451 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
453 #define BT_COLON BT_NMSTRT
454 #include "asciitab.h"
458 STANDARD_VTABLE(sb_) NULL_NORMAL_VTABLE
461 static int unicode_byte_type(char hi, char lo)
463 switch ((unsigned char)hi) {
464 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
466 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
469 switch ((unsigned char)lo) {
479 #define DEFINE_UTF16_TO_UTF8(E) \
481 void E ## toUtf8(const ENCODING *enc ATTR_UNUSED, \
482 const char **fromP, const char *fromLim, \
483 char **toP, const char *toLim) \
486 for (from = *fromP; from != fromLim; from += 2) { \
489 unsigned char lo = GET_LO(from); \
490 unsigned char hi = GET_HI(from); \
494 if (*toP == toLim) { \
502 case 0x1: case 0x2: case 0x3: \
503 case 0x4: case 0x5: case 0x6: case 0x7: \
504 if (toLim - *toP < 2) { \
508 *(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
509 *(*toP)++ = ((lo & 0x3f) | 0x80); \
512 if (toLim - *toP < 3) { \
516 /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
517 *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
518 *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
519 *(*toP)++ = ((lo & 0x3f) | 0x80); \
521 case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
522 if (toLim - *toP < 4) { \
526 plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
527 *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
528 *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
530 lo2 = GET_LO(from); \
531 *(*toP)++ = (((lo & 0x3) << 4) \
532 | ((GET_HI(from) & 0x3) << 2) \
535 *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
542 #define DEFINE_UTF16_TO_UTF16(E) \
544 void E ## toUtf16(const ENCODING *enc ATTR_UNUSED, \
545 const char **fromP, const char *fromLim, \
546 unsigned short **toP, const unsigned short *toLim) \
548 /* Avoid copying first half only of surrogate */ \
549 if (fromLim - *fromP > ((toLim - *toP) << 1) \
550 && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
552 for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
553 *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
556 #define SET2(ptr, ch) \
557 (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
558 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
559 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
561 DEFINE_UTF16_TO_UTF8(little2_)
562 DEFINE_UTF16_TO_UTF16(little2_)
568 #define SET2(ptr, ch) \
569 (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
570 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
571 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
573 DEFINE_UTF16_TO_UTF8(big2_)
574 DEFINE_UTF16_TO_UTF16(big2_)
580 #define LITTLE2_BYTE_TYPE(enc, p) \
582 ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
583 : unicode_byte_type((p)[1], (p)[0]))
584 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
585 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
586 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
587 UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
588 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
589 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
594 int little2_byteType(const ENCODING *enc, const char *p)
596 return LITTLE2_BYTE_TYPE(enc, p);
600 int little2_byteToAscii(const ENCODING *enc, const char *p)
602 return LITTLE2_BYTE_TO_ASCII(enc, p);
606 int little2_charMatches(const ENCODING *enc, const char *p, int c)
608 return LITTLE2_CHAR_MATCHES(enc, p, c);
612 int little2_isNameMin(const ENCODING *enc, const char *p)
614 return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
618 int little2_isNmstrtMin(const ENCODING *enc, const char *p)
620 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
624 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
626 #else /* not XML_MIN_SIZE */
629 #define PREFIX(ident) little2_ ## ident
630 #define MINBPC(enc) 2
631 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
632 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
633 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
634 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
635 #define IS_NAME_CHAR(enc, p, n) 0
636 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
637 #define IS_NMSTRT_CHAR(enc, p, n) (0)
638 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
640 #include "xmltok_impl.c"
647 #undef IS_NAME_CHAR_MINBPC
648 #undef IS_NMSTRT_CHAR
649 #undef IS_NMSTRT_CHAR_MINBPC
650 #undef IS_INVALID_CHAR
652 #endif /* not XML_MIN_SIZE */
656 static const struct normal_encoding little2_encoding_ns = {
658 #if XML_BYTE_ORDER == 12
665 #include "asciitab.h"
666 #include "latin1tab.h"
668 STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
673 static const struct normal_encoding little2_encoding = {
675 #if XML_BYTE_ORDER == 12
682 #define BT_COLON BT_NMSTRT
683 #include "asciitab.h"
685 #include "latin1tab.h"
687 STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
690 #if XML_BYTE_ORDER != 21
694 static const struct normal_encoding internal_little2_encoding_ns = {
697 #include "iasciitab.h"
698 #include "latin1tab.h"
700 STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
705 static const struct normal_encoding internal_little2_encoding = {
708 #define BT_COLON BT_NMSTRT
709 #include "iasciitab.h"
711 #include "latin1tab.h"
713 STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
719 #define BIG2_BYTE_TYPE(enc, p) \
721 ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
722 : unicode_byte_type((p)[0], (p)[1]))
723 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
724 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
725 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
726 UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
727 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
728 UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
733 int big2_byteType(const ENCODING *enc, const char *p)
735 return BIG2_BYTE_TYPE(enc, p);
739 int big2_byteToAscii(const ENCODING *enc, const char *p)
741 return BIG2_BYTE_TO_ASCII(enc, p);
745 int big2_charMatches(const ENCODING *enc, const char *p, int c)
747 return BIG2_CHAR_MATCHES(enc, p, c);
751 int big2_isNameMin(const ENCODING *enc, const char *p)
753 return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
757 int big2_isNmstrtMin(const ENCODING *enc, const char *p)
759 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
763 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
765 #else /* not XML_MIN_SIZE */
768 #define PREFIX(ident) big2_ ## ident
769 #define MINBPC(enc) 2
770 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
771 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
772 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
773 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
774 #define IS_NAME_CHAR(enc, p, n) 0
775 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
776 #define IS_NMSTRT_CHAR(enc, p, n) (0)
777 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
779 #include "xmltok_impl.c"
786 #undef IS_NAME_CHAR_MINBPC
787 #undef IS_NMSTRT_CHAR
788 #undef IS_NMSTRT_CHAR_MINBPC
789 #undef IS_INVALID_CHAR
791 #endif /* not XML_MIN_SIZE */
795 static const struct normal_encoding big2_encoding_ns = {
797 #if XML_BYTE_ORDER == 21
804 #include "asciitab.h"
805 #include "latin1tab.h"
807 STANDARD_VTABLE(big2_) NULL_NORMAL_VTABLE
812 static const struct normal_encoding big2_encoding = {
814 #if XML_BYTE_ORDER == 21
821 #define BT_COLON BT_NMSTRT
822 #include "asciitab.h"
824 #include "latin1tab.h"
826 STANDARD_VTABLE(big2_) NULL_NORMAL_VTABLE
829 #if XML_BYTE_ORDER != 12
833 static const struct normal_encoding internal_big2_encoding_ns = {
836 #include "iasciitab.h"
837 #include "latin1tab.h"
839 STANDARD_VTABLE(big2_)
844 static const struct normal_encoding internal_big2_encoding = {
847 #define BT_COLON BT_NMSTRT
848 #include "iasciitab.h"
850 #include "latin1tab.h"
852 STANDARD_VTABLE(big2_) NULL_NORMAL_VTABLE
860 int streqci(const char *s1, const char *s2)
865 if (ASCII_a <= c1 && c1 <= ASCII_z)
866 c1 += ASCII_A - ASCII_a;
867 if (ASCII_a <= c2 && c2 <= ASCII_z)
868 c2 += ASCII_A - ASCII_a;
878 void initUpdatePosition(const ENCODING *enc ATTR_UNUSED, const char *ptr,
879 const char *end, POSITION *pos)
881 normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
885 int toAscii(const ENCODING *enc, const char *ptr, const char *end)
889 XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
909 /* Return 1 if there's just optional white space
910 or there's an S followed by name=val. */
912 int parsePseudoAttribute(const ENCODING *enc,
915 const char **namePtr,
916 const char **nameEndPtr,
918 const char **nextTokPtr)
926 if (!isSpace(toAscii(enc, ptr, end))) {
931 ptr += enc->minBytesPerChar;
932 } while (isSpace(toAscii(enc, ptr, end)));
939 c = toAscii(enc, ptr, end);
944 if (c == ASCII_EQUALS) {
951 ptr += enc->minBytesPerChar;
952 } while (isSpace(c = toAscii(enc, ptr, end)));
953 if (c != ASCII_EQUALS) {
959 ptr += enc->minBytesPerChar;
961 if (ptr == *namePtr) {
965 ptr += enc->minBytesPerChar;
966 c = toAscii(enc, ptr, end);
968 ptr += enc->minBytesPerChar;
969 c = toAscii(enc, ptr, end);
971 if (c != ASCII_QUOT && c != ASCII_APOS) {
976 ptr += enc->minBytesPerChar;
978 for (;; ptr += enc->minBytesPerChar) {
979 c = toAscii(enc, ptr, end);
982 if (!(ASCII_a <= c && c <= ASCII_z)
983 && !(ASCII_A <= c && c <= ASCII_Z)
984 && !(ASCII_0 <= c && c <= ASCII_9)
987 && c != ASCII_UNDERSCORE) {
992 *nextTokPtr = ptr + enc->minBytesPerChar;
996 static const char KW_version[] = {
997 ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
1000 static const char KW_encoding[] = {
1001 ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
1004 static const char KW_standalone[] = {
1005 ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a, ASCII_l, ASCII_o, ASCII_n, ASCII_e, '\0'
1008 static const char KW_yes[] = {
1009 ASCII_y, ASCII_e, ASCII_s, '\0'
1012 static const char KW_no[] = {
1013 ASCII_n, ASCII_o, '\0'
1017 int doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *,
1020 int isGeneralTextEntity,
1021 const ENCODING *enc,
1024 const char **badPtr,
1025 const char **versionPtr,
1026 const char **encodingName,
1027 const ENCODING **encoding,
1030 const char *val = 0;
1031 const char *name = 0;
1032 const char *nameEnd = 0;
1033 ptr += 5 * enc->minBytesPerChar;
1034 end -= 2 * enc->minBytesPerChar;
1035 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr) || !name) {
1039 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1040 if (!isGeneralTextEntity) {
1048 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1053 if (isGeneralTextEntity) {
1054 /* a TextDecl must have an EncodingDecl */
1061 if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
1062 int c = toAscii(enc, val, end);
1063 if (!(ASCII_a <= c && c <= ASCII_z) && !(ASCII_A <= c && c <= ASCII_Z)) {
1068 *encodingName = val;
1070 *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
1071 if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1078 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone) || isGeneralTextEntity) {
1082 if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1086 else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1094 while (isSpace(toAscii(enc, ptr, end)))
1095 ptr += enc->minBytesPerChar;
1104 int checkCharRefNumber(int result)
1106 switch (result >> 8) {
1107 case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1108 case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1111 if (latin1_encoding.type[result] == BT_NONXML)
1115 if (result == 0xFFFE || result == 0xFFFF)
1122 int xmlrpc_XmlUtf8Encode(int c, char *buf)
1125 /* minN is minimum legal resulting value for N byte sequence */
1134 buf[0] = (c | UTF8_cval1);
1138 buf[0] = ((c >> 6) | UTF8_cval2);
1139 buf[1] = ((c & 0x3f) | 0x80);
1143 buf[0] = ((c >> 12) | UTF8_cval3);
1144 buf[1] = (((c >> 6) & 0x3f) | 0x80);
1145 buf[2] = ((c & 0x3f) | 0x80);
1149 buf[0] = ((c >> 18) | UTF8_cval4);
1150 buf[1] = (((c >> 12) & 0x3f) | 0x80);
1151 buf[2] = (((c >> 6) & 0x3f) | 0x80);
1152 buf[3] = ((c & 0x3f) | 0x80);
1158 int xmlrpc_XmlUtf16Encode(int charNum, unsigned short *buf)
1162 if (charNum < 0x10000) {
1166 if (charNum < 0x110000) {
1168 buf[0] = (charNum >> 10) + 0xD800;
1169 buf[1] = (charNum & 0x3FF) + 0xDC00;
1175 struct unknown_encoding {
1176 struct normal_encoding normal;
1177 int (*convert)(void *userData, const char *p);
1179 unsigned short utf16[256];
1183 int xmlrpc_XmlSizeOfUnknownEncoding(void)
1185 return sizeof(struct unknown_encoding);
1189 int unknown_isName(const ENCODING *enc, const char *p)
1191 int c = ((const struct unknown_encoding *)enc)
1192 ->convert(((const struct unknown_encoding *)enc)->userData, p);
1195 return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
1199 int unknown_isNmstrt(const ENCODING *enc, const char *p)
1201 int c = ((const struct unknown_encoding *)enc)
1202 ->convert(((const struct unknown_encoding *)enc)->userData, p);
1205 return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
1209 int unknown_isInvalid(const ENCODING *enc, const char *p)
1211 int c = ((const struct unknown_encoding *)enc)
1212 ->convert(((const struct unknown_encoding *)enc)->userData, p);
1213 return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1217 void unknown_toUtf8(const ENCODING *enc,
1218 const char **fromP, const char *fromLim,
1219 char **toP, const char *toLim)
1221 char buf[XML_UTF8_ENCODE_MAX];
1225 if (*fromP == fromLim)
1227 utf8 = ((const struct unknown_encoding *)enc)->utf8[(unsigned char)**fromP];
1230 int c = ((const struct unknown_encoding *)enc)
1231 ->convert(((const struct unknown_encoding *)enc)->userData, *fromP);
1232 n = xmlrpc_XmlUtf8Encode(c, buf);
1233 if (n > toLim - *toP)
1236 *fromP += ((const struct normal_encoding *)enc)->type[(unsigned char)**fromP]
1240 if (n > toLim - *toP)
1245 *(*toP)++ = *utf8++;
1251 void unknown_toUtf16(const ENCODING *enc,
1252 const char **fromP, const char *fromLim,
1253 unsigned short **toP, const unsigned short *toLim)
1255 while (*fromP != fromLim && *toP != toLim) {
1257 = ((const struct unknown_encoding *)enc)->utf16[(unsigned char)**fromP];
1259 c = (unsigned short)((const struct unknown_encoding *)enc)
1260 ->convert(((const struct unknown_encoding *)enc)->userData, *fromP);
1261 *fromP += ((const struct normal_encoding *)enc)->type[(unsigned char)**fromP]
1271 xmlrpc_XmlInitUnknownEncoding(void *mem,
1273 int (*convert)(void *userData, const char *p),
1277 struct unknown_encoding *e = mem;
1278 for (i = 0; i < (int)sizeof(struct normal_encoding); i++)
1279 ((char *)mem)[i] = ((char *)&latin1_encoding)[i];
1280 for (i = 0; i < 128; i++)
1281 if (latin1_encoding.type[i] != BT_OTHER
1282 && latin1_encoding.type[i] != BT_NONXML
1285 for (i = 0; i < 256; i++) {
1288 e->normal.type[i] = BT_MALFORM;
1289 /* This shouldn't really get used. */
1290 e->utf16[i] = 0xFFFF;
1297 e->normal.type[i] = BT_LEAD2 - (c + 2);
1301 else if (c < 0x80) {
1302 if (latin1_encoding.type[c] != BT_OTHER
1303 && latin1_encoding.type[c] != BT_NONXML
1306 e->normal.type[i] = latin1_encoding.type[c];
1308 e->utf8[i][1] = (char)c;
1309 e->utf16[i] = c == 0 ? 0xFFFF : c;
1311 else if (checkCharRefNumber(c) < 0) {
1312 e->normal.type[i] = BT_NONXML;
1313 /* This shouldn't really get used. */
1314 e->utf16[i] = 0xFFFF;
1321 if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
1322 e->normal.type[i] = BT_NMSTRT;
1323 else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
1324 e->normal.type[i] = BT_NAME;
1326 e->normal.type[i] = BT_OTHER;
1327 e->utf8[i][0] = (char)xmlrpc_XmlUtf8Encode(c, e->utf8[i] + 1);
1331 e->userData = userData;
1332 e->convert = convert;
1334 e->normal.isName2 = unknown_isName;
1335 e->normal.isName3 = unknown_isName;
1336 e->normal.isName4 = unknown_isName;
1337 e->normal.isNmstrt2 = unknown_isNmstrt;
1338 e->normal.isNmstrt3 = unknown_isNmstrt;
1339 e->normal.isNmstrt4 = unknown_isNmstrt;
1340 e->normal.isInvalid2 = unknown_isInvalid;
1341 e->normal.isInvalid3 = unknown_isInvalid;
1342 e->normal.isInvalid4 = unknown_isInvalid;
1344 e->normal.enc.utf8Convert = unknown_toUtf8;
1345 e->normal.enc.utf16Convert = unknown_toUtf16;
1346 return &(e->normal.enc);
1349 /* If this enumeration is changed, getEncodingIndex and encodings
1350 must also be changed. */
1359 /* must match encodingNames up to here */
1363 static const char KW_ISO_8859_1[] = {
1364 ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8, ASCII_5, ASCII_9, ASCII_MINUS, ASCII_1, '\0'
1366 static const char KW_US_ASCII[] = {
1367 ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S, ASCII_C, ASCII_I, ASCII_I, '\0'
1369 static const char KW_UTF_8[] = {
1370 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
1372 static const char KW_UTF_16[] = {
1373 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
1375 static const char KW_UTF_16BE[] = {
1376 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_B, ASCII_E, '\0'
1378 static const char KW_UTF_16LE[] = {
1379 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_L, ASCII_E, '\0'
1383 int getEncodingIndex(const char *name)
1385 static const char *encodingNames[] = {
1396 for (i = 0; i < (int)(sizeof(encodingNames)/sizeof(encodingNames[0])); i++)
1397 if (streqci(name, encodingNames[i]))
1402 /* For binary compatibility, we store the index of the encoding specified
1403 at initialization in the isUtf16 member. */
1405 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1406 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1408 /* This is what detects the encoding.
1409 encodingTable maps from encoding indices to encodings;
1410 INIT_ENC_INDEX(enc) is the index of the external (protocol) specified encoding;
1411 state is XML_CONTENT_STATE if we're parsing an external text entity,
1412 and XML_PROLOG_STATE otherwise.
1417 int initScan(const ENCODING **encodingTable,
1418 const INIT_ENCODING *enc,
1422 const char **nextTokPtr)
1424 const ENCODING **encPtr;
1427 return XML_TOK_NONE;
1428 encPtr = enc->encPtr;
1429 if (ptr + 1 == end) {
1430 /* only a single byte available for auto-detection */
1431 #ifndef XML_DTD /* FIXME */
1432 /* a well-formed document entity must have more than one byte */
1433 if (state != XML_CONTENT_STATE)
1434 return XML_TOK_PARTIAL;
1436 /* so we're parsing an external text entity... */
1437 /* if UTF-16 was externally specified, then we need at least 2 bytes */
1438 switch (INIT_ENC_INDEX(enc)) {
1442 return XML_TOK_PARTIAL;
1444 switch ((unsigned char)*ptr) {
1447 case 0xEF: /* possibly first byte of UTF-8 BOM */
1448 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1449 && state == XML_CONTENT_STATE)
1454 return XML_TOK_PARTIAL;
1458 switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
1460 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1461 && state == XML_CONTENT_STATE)
1463 *nextTokPtr = ptr + 2;
1464 *encPtr = encodingTable[UTF_16BE_ENC];
1466 /* 00 3C is handled in the default case */
1468 if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
1469 || INIT_ENC_INDEX(enc) == UTF_16_ENC)
1470 && state == XML_CONTENT_STATE)
1472 *encPtr = encodingTable[UTF_16LE_ENC];
1473 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1475 if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1476 && state == XML_CONTENT_STATE)
1478 *nextTokPtr = ptr + 2;
1479 *encPtr = encodingTable[UTF_16LE_ENC];
1482 /* Maybe a UTF-8 BOM (EF BB BF) */
1483 /* If there's an explicitly specified (external) encoding
1484 of ISO-8859-1 or some flavour of UTF-16
1485 and this is an external text entity,
1486 don't look for the BOM,
1487 because it might be a legal data. */
1488 if (state == XML_CONTENT_STATE) {
1489 int e = INIT_ENC_INDEX(enc);
1490 if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC || e == UTF_16LE_ENC || e == UTF_16_ENC)
1494 return XML_TOK_PARTIAL;
1495 if ((unsigned char)ptr[2] == 0xBF) {
1496 *encPtr = encodingTable[UTF_8_ENC];
1501 if (ptr[0] == '\0') {
1502 /* 0 isn't a legal data character. Furthermore a document entity can only
1503 start with ASCII characters. So the only way this can fail to be big-endian
1504 UTF-16 if it it's an external parsed general entity that's labelled as
1506 if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
1508 *encPtr = encodingTable[UTF_16BE_ENC];
1509 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1511 else if (ptr[1] == '\0') {
1512 /* We could recover here in the case:
1513 - parsing an external entity
1515 - no externally specified encoding
1516 - no encoding declaration
1517 by assuming UTF-16LE. But we don't, because this would mean when
1518 presented just with a single byte, we couldn't reliably determine
1519 whether we needed further bytes. */
1520 if (state == XML_CONTENT_STATE)
1522 *encPtr = encodingTable[UTF_16LE_ENC];
1523 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1528 *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1529 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1535 #include "xmltok_ns.c"
1541 #define NS(x) x ## NS
1542 #define ns(x) x ## _ns
1544 #include "xmltok_ns.c"
1550 xmlrpc_XmlInitUnknownEncodingNS(void *mem,
1552 int (*convert)(void *userData, const char *p),
1555 ENCODING *enc = xmlrpc_XmlInitUnknownEncoding(mem, table, convert, userData);
1557 ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;