initial load of upstream version 1.06.32
[xmlrpc-c] / lib / expat / xmltok / xmltok.c
1 /*
2 Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
3 See the file copying.txt for copying permission.
4 */
5
6 #include "xmlrpc_config.h"
7 #include "xmldef.h"
8 #include "xmltok.h"
9 #include "nametab.h"
10
11 #ifdef XML_DTD
12 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
13 #else
14 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
15 #endif
16
17 #define VTABLE1 \
18   { PREFIX(prologTok), PREFIX(contentTok), \
19     PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
20   { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
21   PREFIX(sameName), \
22   PREFIX(nameMatchesAscii), \
23   PREFIX(nameLength), \
24   PREFIX(skipS), \
25   PREFIX(getAtts), \
26   PREFIX(charRefNumber), \
27   PREFIX(predefinedEntityName), \
28   PREFIX(updatePosition), \
29   PREFIX(isPublicId)
30
31 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
32
33 #define UCS2_GET_NAMING(pages, hi, lo) \
34    (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
35
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)))
45
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)] \
53                        << 3) \
54                       + ((((byte)[1]) & 3) << 1) \
55                       + ((((byte)[2]) >> 5) & 1)] \
56          & (1 << (((byte)[2]) & 0x1F)))
57
58 #define UTF8_GET_NAMING(pages, p, n) \
59   ((n) == 2 \
60   ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
61   : ((n) == 3 \
62      ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
63      : 0))
64
65 #define UTF8_INVALID3(p) \
66   ((*p) == 0xED \
67   ? (((p)[1] & 0x20) != 0) \
68   : ((*p) == 0xEF \
69      ? ((p)[1] == 0xBF && ((p)[2] == 0xBF || (p)[2] == 0xBE)) \
70      : 0))
71
72 #define UTF8_INVALID4(p) ((*p) == 0xF4 && ((p)[1] & 0x30) != 0)
73
74 static
75 int isNever(const ENCODING *enc ATTR_UNUSED, const char *p ATTR_UNUSED)
76 {
77   return 0;
78 }
79
80 static
81 int utf8_isName2(const ENCODING *enc ATTR_UNUSED, const char *p)
82 {
83   return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
84 }
85
86 static
87 int utf8_isName3(const ENCODING *enc ATTR_UNUSED, const char *p)
88 {
89   return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
90 }
91
92 #define utf8_isName4 isNever
93
94 static
95 int utf8_isNmstrt2(const ENCODING *enc ATTR_UNUSED, const char *p)
96 {
97   return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
98 }
99
100 static
101 int utf8_isNmstrt3(const ENCODING *enc ATTR_UNUSED, const char *p)
102 {
103   return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
104 }
105
106 #define utf8_isNmstrt4 isNever
107
108 #define utf8_isInvalid2 isNever
109
110 static
111 int utf8_isInvalid3(const ENCODING *enc ATTR_UNUSED, const char *p)
112 {
113   return UTF8_INVALID3((const unsigned char *)p);
114 }
115
116 static
117 int utf8_isInvalid4(const ENCODING *enc ATTR_UNUSED, const char *p)
118 {
119   return UTF8_INVALID4((const unsigned char *)p);
120 }
121
122 struct normal_encoding {
123   ENCODING enc;
124   unsigned char type[256];
125 #ifdef XML_MIN_SIZE
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 *);
141 };
142
143 #ifdef XML_MIN_SIZE
144
145 #define STANDARD_VTABLE(E) \
146  E ## byteType, \
147  E ## isNameMin, \
148  E ## isNmstrtMin, \
149  E ## byteToAscii, \
150  E ## charMatches,
151
152 #else
153
154 #define STANDARD_VTABLE(E) /* as nothing */
155
156 #endif
157
158 #define NORMAL_VTABLE(E) \
159  E ## isName2, \
160  E ## isName3, \
161  E ## isName4, \
162  E ## isNmstrt2, \
163  E ## isNmstrt3, \
164  E ## isNmstrt4, \
165  E ## isInvalid2, \
166  E ## isInvalid3, \
167  E ## isInvalid4
168
169 #define NULL_NORMAL_VTABLE \
170   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
171
172 static int checkCharRefNumber(int);
173
174 #include "xmltok_impl.h"
175 #include "ascii.h"
176
177 #ifdef XML_MIN_SIZE
178 #define sb_isNameMin isNever
179 #define sb_isNmstrtMin isNever
180 #endif
181
182 #ifdef XML_MIN_SIZE
183 #define MINBPC(enc) ((enc)->minBytesPerChar)
184 #else
185 /* minimum bytes per character */
186 #define MINBPC(enc) 1
187 #endif
188
189 #define SB_BYTE_TYPE(enc, p) \
190   (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
191
192 #ifdef XML_MIN_SIZE
193 static
194 int sb_byteType(const ENCODING *enc, const char *p)
195 {
196   return SB_BYTE_TYPE(enc, p);
197 }
198 #define BYTE_TYPE(enc, p) \
199  (((const struct normal_encoding *)(enc))->byteType(enc, p))
200 #else
201 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
202 #endif
203
204 #ifdef XML_MIN_SIZE
205 #define BYTE_TO_ASCII(enc, p) \
206  (((const struct normal_encoding *)(enc))->byteToAscii(enc, p))
207 static
208 int sb_byteToAscii(const ENCODING *enc, const char *p)
209 {
210   return *p;
211 }
212 #else
213 #define BYTE_TO_ASCII(enc, p) (*(p))
214 #endif
215
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))
222
223 #ifdef XML_MIN_SIZE
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))
228 #else
229 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
230 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
231 #endif
232
233 #ifdef XML_MIN_SIZE
234 #define CHAR_MATCHES(enc, p, c) \
235  (((const struct normal_encoding *)(enc))->charMatches(enc, p, c))
236 static
237 int sb_charMatches(const ENCODING *enc, const char *p, int c)
238 {
239   return *p == c;
240 }
241 #else
242 /* c is an ASCII character */
243 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
244 #endif
245
246 #define PREFIX(ident) normal_ ## ident
247 #include "xmltok_impl.c"
248
249 #undef MINBPC
250 #undef BYTE_TYPE
251 #undef BYTE_TO_ASCII
252 #undef CHAR_MATCHES
253 #undef IS_NAME_CHAR
254 #undef IS_NAME_CHAR_MINBPC
255 #undef IS_NMSTRT_CHAR
256 #undef IS_NMSTRT_CHAR_MINBPC
257 #undef IS_INVALID_CHAR
258
259 enum {  /* UTF8_cvalN is value of masked first byte of N byte sequence */
260   UTF8_cval1 = 0x00,
261   UTF8_cval2 = 0xc0,
262   UTF8_cval3 = 0xe0,
263   UTF8_cval4 = 0xf0
264 };
265
266 static
267 void utf8_toUtf8(const ENCODING * enc ATTR_UNUSED,
268                  const char **fromP, const char *fromLim,
269                  char **toP, const char *toLim)
270 {
271   char *to;
272   const char *from;
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)
277         break;
278   }
279   for (to = *toP, from = *fromP; from != fromLim; from++, to++)
280     *to = *from;
281   *fromP = from;
282   *toP = to;
283 }
284
285 static
286 void utf8_toUtf16(const ENCODING *enc,
287                   const char **fromP, const char *fromLim,
288                   unsigned short **toP, const unsigned short *toLim)
289 {
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]) {
294     case BT_LEAD2:
295       *to++ = ((from[0] & 0x1f) << 6) | (from[1] & 0x3f);
296       from += 2;
297       break;
298     case BT_LEAD3:
299       *to++ = ((from[0] & 0xf) << 12) | ((from[1] & 0x3f) << 6) | (from[2] & 0x3f);
300       from += 3;
301       break;
302     case BT_LEAD4:
303       {
304         unsigned long n;
305         if (to + 1 == toLim)
306           break;
307         n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12) | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
308         n -= 0x10000;
309         to[0] = (unsigned short)((n >> 10) | 0xD800);
310         to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
311         to += 2;
312         from += 4;
313       }
314       break;
315     default:
316       *to++ = *from++;
317       break;
318     }
319   }
320   *fromP = from;
321   *toP = to;
322 }
323
324 #ifdef XML_NS
325 static const struct normal_encoding utf8_encoding_ns = {
326   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
327   {
328 #include "asciitab.h"
329 #include "utf8tab.h"
330   },
331   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
332 };
333 #endif
334
335 static const struct normal_encoding utf8_encoding = {
336   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
337   {
338 #define BT_COLON BT_NMSTRT
339 #include "asciitab.h"
340 #undef BT_COLON
341 #include "utf8tab.h"
342   },
343   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
344 };
345
346 #ifdef XML_NS
347
348 static const struct normal_encoding internal_utf8_encoding_ns = {
349   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
350   {
351 #include "iasciitab.h"
352 #include "utf8tab.h"
353   },
354   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
355 };
356
357 #endif
358
359 static const struct normal_encoding internal_utf8_encoding = {
360   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
361   {
362 #define BT_COLON BT_NMSTRT
363 #include "iasciitab.h"
364 #undef BT_COLON
365 #include "utf8tab.h"
366   },
367   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
368 };
369
370 static
371 void latin1_toUtf8(const ENCODING *enc ATTR_UNUSED,
372                    const char **fromP, const char *fromLim,
373                    char **toP, const char *toLim)
374 {
375   for (;;) {
376     unsigned char c;
377     if (*fromP == fromLim)
378       break;
379     c = (unsigned char)**fromP;
380     if (c & 0x80) {
381       if (toLim - *toP < 2)
382         break;
383       *(*toP)++ = ((c >> 6) | UTF8_cval2);
384       *(*toP)++ = ((c & 0x3f) | 0x80);
385       (*fromP)++;
386     }
387     else {
388       if (*toP == toLim)
389         break;
390       *(*toP)++ = *(*fromP)++;
391     }
392   }
393 }
394
395 static
396 void latin1_toUtf16(const ENCODING *enc ATTR_UNUSED,
397                     const char **fromP, const char *fromLim,
398                     unsigned short **toP, const unsigned short *toLim)
399 {
400   while (*fromP != fromLim && *toP != toLim)
401     *(*toP)++ = (unsigned char)*(*fromP)++;
402 }
403
404 #ifdef XML_NS
405
406 static const struct normal_encoding latin1_encoding_ns = {
407   { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
408   {
409 #include "asciitab.h"
410 #include "latin1tab.h"
411   },
412   STANDARD_VTABLE(sb_) NULL_NORMAL_VTABLE
413 };
414
415 #endif
416
417 static const struct normal_encoding latin1_encoding = {
418   { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
419   {
420 #define BT_COLON BT_NMSTRT
421 #include "asciitab.h"
422 #undef BT_COLON
423 #include "latin1tab.h"
424   },
425   STANDARD_VTABLE(sb_) NULL_NORMAL_VTABLE
426 };
427
428 static
429 void ascii_toUtf8(const ENCODING *enc ATTR_UNUSED,
430                   const char **fromP, const char *fromLim,
431                   char **toP, const char *toLim)
432 {
433   while (*fromP != fromLim && *toP != toLim)
434     *(*toP)++ = *(*fromP)++;
435 }
436
437 #ifdef XML_NS
438
439 static const struct normal_encoding ascii_encoding_ns = {
440   { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
441   {
442 #include "asciitab.h"
443 /* BT_NONXML == 0 */
444   },
445   STANDARD_VTABLE(sb_)
446 };
447
448 #endif
449
450 static const struct normal_encoding ascii_encoding = {
451   { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
452   {
453 #define BT_COLON BT_NMSTRT
454 #include "asciitab.h"
455 #undef BT_COLON
456 /* BT_NONXML == 0 */
457   },
458   STANDARD_VTABLE(sb_) NULL_NORMAL_VTABLE
459 };
460
461 static int unicode_byte_type(char hi, char lo)
462 {
463   switch ((unsigned char)hi) {
464   case 0xD8: case 0xD9: case 0xDA: case 0xDB:
465     return BT_LEAD4;
466   case 0xDC: case 0xDD: case 0xDE: case 0xDF:
467     return BT_TRAIL;
468   case 0xFF:
469     switch ((unsigned char)lo) {
470     case 0xFF:
471     case 0xFE:
472       return BT_NONXML;
473     }
474     break;
475   }
476   return BT_NONASCII;
477 }
478
479 #define DEFINE_UTF16_TO_UTF8(E) \
480 static \
481 void E ## toUtf8(const ENCODING *enc ATTR_UNUSED, \
482                  const char **fromP, const char *fromLim, \
483                  char **toP, const char *toLim) \
484 { \
485   const char *from; \
486   for (from = *fromP; from != fromLim; from += 2) { \
487     int plane; \
488     unsigned char lo2; \
489     unsigned char lo = GET_LO(from); \
490     unsigned char hi = GET_HI(from); \
491     switch (hi) { \
492     case 0: \
493       if (lo < 0x80) { \
494         if (*toP == toLim) { \
495           *fromP = from; \
496           return; \
497         } \
498         *(*toP)++ = lo; \
499         break; \
500       } \
501       /* fall through */ \
502     case 0x1: case 0x2: case 0x3: \
503     case 0x4: case 0x5: case 0x6: case 0x7: \
504       if (toLim -  *toP < 2) { \
505         *fromP = from; \
506         return; \
507       } \
508       *(*toP)++ = ((lo >> 6) | (hi << 2) |  UTF8_cval2); \
509       *(*toP)++ = ((lo & 0x3f) | 0x80); \
510       break; \
511     default: \
512       if (toLim -  *toP < 3)  { \
513         *fromP = from; \
514         return; \
515       } \
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); \
520       break; \
521     case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
522       if (toLim -  *toP < 4) { \
523         *fromP = from; \
524         return; \
525       } \
526       plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
527       *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
528       *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
529       from += 2; \
530       lo2 = GET_LO(from); \
531       *(*toP)++ = (((lo & 0x3) << 4) \
532                    | ((GET_HI(from) & 0x3) << 2) \
533                    | (lo2 >> 6) \
534                    | 0x80); \
535       *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
536       break; \
537     } \
538   } \
539   *fromP = from; \
540 }
541
542 #define DEFINE_UTF16_TO_UTF16(E) \
543 static \
544 void E ## toUtf16(const ENCODING *enc ATTR_UNUSED, \
545                   const char **fromP, const char *fromLim, \
546                   unsigned short **toP, const unsigned short *toLim) \
547 { \
548   /* Avoid copying first half only of surrogate */ \
549   if (fromLim - *fromP > ((toLim - *toP) << 1) \
550       && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
551     fromLim -= 2; \
552   for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
553     *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
554 }
555
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])
560
561 DEFINE_UTF16_TO_UTF8(little2_)
562 DEFINE_UTF16_TO_UTF16(little2_)
563
564 #undef SET2
565 #undef GET_LO
566 #undef GET_HI
567
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])
572
573 DEFINE_UTF16_TO_UTF8(big2_)
574 DEFINE_UTF16_TO_UTF16(big2_)
575
576 #undef SET2
577 #undef GET_LO
578 #undef GET_HI
579
580 #define LITTLE2_BYTE_TYPE(enc, p) \
581  ((p)[1] == 0 \
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])
590
591 #ifdef XML_MIN_SIZE
592
593 static
594 int little2_byteType(const ENCODING *enc, const char *p)
595 {
596   return LITTLE2_BYTE_TYPE(enc, p);
597 }
598
599 static
600 int little2_byteToAscii(const ENCODING *enc, const char *p)
601 {
602   return LITTLE2_BYTE_TO_ASCII(enc, p);
603 }
604
605 static
606 int little2_charMatches(const ENCODING *enc, const char *p, int c)
607 {
608   return LITTLE2_CHAR_MATCHES(enc, p, c);
609 }
610
611 static
612 int little2_isNameMin(const ENCODING *enc, const char *p)
613 {
614   return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
615 }
616
617 static
618 int little2_isNmstrtMin(const ENCODING *enc, const char *p)
619 {
620   return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
621 }
622
623 #undef VTABLE
624 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
625
626 #else /* not XML_MIN_SIZE */
627
628 #undef PREFIX
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)
639
640 #include "xmltok_impl.c"
641
642 #undef MINBPC
643 #undef BYTE_TYPE
644 #undef BYTE_TO_ASCII
645 #undef CHAR_MATCHES
646 #undef IS_NAME_CHAR
647 #undef IS_NAME_CHAR_MINBPC
648 #undef IS_NMSTRT_CHAR
649 #undef IS_NMSTRT_CHAR_MINBPC
650 #undef IS_INVALID_CHAR
651
652 #endif /* not XML_MIN_SIZE */
653
654 #ifdef XML_NS
655
656 static const struct normal_encoding little2_encoding_ns = { 
657   { VTABLE, 2, 0,
658 #if XML_BYTE_ORDER == 12
659     1
660 #else
661     0
662 #endif
663   },
664   {
665 #include "asciitab.h"
666 #include "latin1tab.h"
667   },
668   STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
669 };
670
671 #endif
672
673 static const struct normal_encoding little2_encoding = { 
674   { VTABLE, 2, 0,
675 #if XML_BYTE_ORDER == 12
676     1
677 #else
678     0
679 #endif
680   },
681   {
682 #define BT_COLON BT_NMSTRT
683 #include "asciitab.h"
684 #undef BT_COLON
685 #include "latin1tab.h"
686   },
687   STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
688 };
689
690 #if XML_BYTE_ORDER != 21
691
692 #ifdef XML_NS
693
694 static const struct normal_encoding internal_little2_encoding_ns = { 
695   { VTABLE, 2, 0, 1 },
696   {
697 #include "iasciitab.h"
698 #include "latin1tab.h"
699   },
700   STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
701 };
702
703 #endif
704
705 static const struct normal_encoding internal_little2_encoding = { 
706   { VTABLE, 2, 0, 1 },
707   {
708 #define BT_COLON BT_NMSTRT
709 #include "iasciitab.h"
710 #undef BT_COLON
711 #include "latin1tab.h"
712   },
713   STANDARD_VTABLE(little2_) NULL_NORMAL_VTABLE
714 };
715
716 #endif
717
718
719 #define BIG2_BYTE_TYPE(enc, p) \
720  ((p)[0] == 0 \
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])
729
730 #ifdef XML_MIN_SIZE
731
732 static
733 int big2_byteType(const ENCODING *enc, const char *p)
734 {
735   return BIG2_BYTE_TYPE(enc, p);
736 }
737
738 static
739 int big2_byteToAscii(const ENCODING *enc, const char *p)
740 {
741   return BIG2_BYTE_TO_ASCII(enc, p);
742 }
743
744 static
745 int big2_charMatches(const ENCODING *enc, const char *p, int c)
746 {
747   return BIG2_CHAR_MATCHES(enc, p, c);
748 }
749
750 static
751 int big2_isNameMin(const ENCODING *enc, const char *p)
752 {
753   return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
754 }
755
756 static
757 int big2_isNmstrtMin(const ENCODING *enc, const char *p)
758 {
759   return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
760 }
761
762 #undef VTABLE
763 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
764
765 #else /* not XML_MIN_SIZE */
766
767 #undef PREFIX
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)
778
779 #include "xmltok_impl.c"
780
781 #undef MINBPC
782 #undef BYTE_TYPE
783 #undef BYTE_TO_ASCII
784 #undef CHAR_MATCHES
785 #undef IS_NAME_CHAR
786 #undef IS_NAME_CHAR_MINBPC
787 #undef IS_NMSTRT_CHAR
788 #undef IS_NMSTRT_CHAR_MINBPC
789 #undef IS_INVALID_CHAR
790
791 #endif /* not XML_MIN_SIZE */
792
793 #ifdef XML_NS
794
795 static const struct normal_encoding big2_encoding_ns = {
796   { VTABLE, 2, 0,
797 #if XML_BYTE_ORDER == 21
798   1
799 #else
800   0
801 #endif
802   },
803   {
804 #include "asciitab.h"
805 #include "latin1tab.h"
806   },
807   STANDARD_VTABLE(big2_) NULL_NORMAL_VTABLE
808 };
809
810 #endif
811
812 static const struct normal_encoding big2_encoding = {
813   { VTABLE, 2, 0,
814 #if XML_BYTE_ORDER == 21
815   1
816 #else
817   0
818 #endif
819   },
820   {
821 #define BT_COLON BT_NMSTRT
822 #include "asciitab.h"
823 #undef BT_COLON
824 #include "latin1tab.h"
825   },
826   STANDARD_VTABLE(big2_) NULL_NORMAL_VTABLE
827 };
828
829 #if XML_BYTE_ORDER != 12
830
831 #ifdef XML_NS
832
833 static const struct normal_encoding internal_big2_encoding_ns = {
834   { VTABLE, 2, 0, 1 },
835   {
836 #include "iasciitab.h"
837 #include "latin1tab.h"
838   },
839   STANDARD_VTABLE(big2_)
840 };
841
842 #endif
843
844 static const struct normal_encoding internal_big2_encoding = {
845   { VTABLE, 2, 0, 1 },
846   {
847 #define BT_COLON BT_NMSTRT
848 #include "iasciitab.h"
849 #undef BT_COLON
850 #include "latin1tab.h"
851   },
852   STANDARD_VTABLE(big2_) NULL_NORMAL_VTABLE
853 };
854
855 #endif
856
857 #undef PREFIX
858
859 static
860 int streqci(const char *s1, const char *s2)
861 {
862   for (;;) {
863     char c1 = *s1++;
864     char c2 = *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;
869     if (c1 != c2)
870       return 0;
871     if (!c1)
872       break;
873   }
874   return 1;
875 }
876
877 static
878 void initUpdatePosition(const ENCODING *enc ATTR_UNUSED, const char *ptr,
879                         const char *end, POSITION *pos)
880 {
881   normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
882 }
883
884 static
885 int toAscii(const ENCODING *enc, const char *ptr, const char *end)
886 {
887   char buf[1];
888   char *p = buf;
889   XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
890   if (p == buf)
891     return -1;
892   else
893     return buf[0];
894 }
895
896 static
897 int isSpace(int c)
898 {
899   switch (c) {
900   case 0x20:
901   case 0xD:
902   case 0xA:
903   case 0x9:     
904     return 1;
905   }
906   return 0;
907 }
908
909 /* Return 1 if there's just optional white space
910 or there's an S followed by name=val. */
911 static
912 int parsePseudoAttribute(const ENCODING *enc,
913                          const char *ptr,
914                          const char *end,
915                          const char **namePtr,
916                          const char **nameEndPtr,
917                          const char **valPtr,
918                          const char **nextTokPtr)
919 {
920   int c;
921   char open;
922   if (ptr == end) {
923     *namePtr = 0;
924     return 1;
925   }
926   if (!isSpace(toAscii(enc, ptr, end))) {
927     *nextTokPtr = ptr;
928     return 0;
929   }
930   do {
931     ptr += enc->minBytesPerChar;
932   } while (isSpace(toAscii(enc, ptr, end)));
933   if (ptr == end) {
934     *namePtr = 0;
935     return 1;
936   }
937   *namePtr = ptr;
938   for (;;) {
939     c = toAscii(enc, ptr, end);
940     if (c == -1) {
941       *nextTokPtr = ptr;
942       return 0;
943     }
944     if (c == ASCII_EQUALS) {
945       *nameEndPtr = ptr;
946       break;
947     }
948     if (isSpace(c)) {
949       *nameEndPtr = ptr;
950       do {
951         ptr += enc->minBytesPerChar;
952       } while (isSpace(c = toAscii(enc, ptr, end)));
953       if (c != ASCII_EQUALS) {
954         *nextTokPtr = ptr;
955         return 0;
956       }
957       break;
958     }
959     ptr += enc->minBytesPerChar;
960   }
961   if (ptr == *namePtr) {
962     *nextTokPtr = ptr;
963     return 0;
964   }
965   ptr += enc->minBytesPerChar;
966   c = toAscii(enc, ptr, end);
967   while (isSpace(c)) {
968     ptr += enc->minBytesPerChar;
969     c = toAscii(enc, ptr, end);
970   }
971   if (c != ASCII_QUOT && c != ASCII_APOS) {
972     *nextTokPtr = ptr;
973     return 0;
974   }
975   open = c;
976   ptr += enc->minBytesPerChar;
977   *valPtr = ptr;
978   for (;; ptr += enc->minBytesPerChar) {
979     c = toAscii(enc, ptr, end);
980     if (c == open)
981       break;
982     if (!(ASCII_a <= c && c <= ASCII_z)
983         && !(ASCII_A <= c && c <= ASCII_Z)
984         && !(ASCII_0 <= c && c <= ASCII_9)
985         && c != ASCII_PERIOD
986         && c != ASCII_MINUS
987         && c != ASCII_UNDERSCORE) {
988       *nextTokPtr = ptr;
989       return 0;
990     }
991   }
992   *nextTokPtr = ptr + enc->minBytesPerChar;
993   return 1;
994 }
995
996 static const char KW_version[] = {
997   ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
998 };
999
1000 static const char KW_encoding[] = {
1001   ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
1002 };
1003
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'
1006 };
1007
1008 static const char KW_yes[] = {
1009   ASCII_y, ASCII_e, ASCII_s,  '\0'
1010 };
1011
1012 static const char KW_no[] = {
1013   ASCII_n, ASCII_o,  '\0'
1014 };
1015
1016 static
1017 int doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *,
1018                                                      const char *,
1019                                                      const char *),
1020                    int isGeneralTextEntity,
1021                    const ENCODING *enc,
1022                    const char *ptr,
1023                    const char *end,
1024                    const char **badPtr,
1025                    const char **versionPtr,
1026                    const char **encodingName,
1027                    const ENCODING **encoding,
1028                    int *standalone)
1029 {
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) {
1036     *badPtr = ptr;
1037     return 0;
1038   }
1039   if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1040     if (!isGeneralTextEntity) {
1041       *badPtr = name;
1042       return 0;
1043     }
1044   }
1045   else {
1046     if (versionPtr)
1047       *versionPtr = val;
1048     if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1049       *badPtr = ptr;
1050       return 0;
1051     }
1052     if (!name) {
1053       if (isGeneralTextEntity) {
1054         /* a TextDecl must have an EncodingDecl */
1055         *badPtr = ptr;
1056         return 0;
1057       }
1058       return 1;
1059     }
1060   }
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)) {
1064       *badPtr = val;
1065       return 0;
1066     }
1067     if (encodingName)
1068       *encodingName = val;
1069     if (encoding)
1070       *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
1071     if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1072       *badPtr = ptr;
1073       return 0;
1074     }
1075     if (!name)
1076       return 1;
1077   }
1078   if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone) || isGeneralTextEntity) {
1079     *badPtr = name;
1080     return 0;
1081   }
1082   if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1083     if (standalone)
1084       *standalone = 1;
1085   }
1086   else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1087     if (standalone)
1088       *standalone = 0;
1089   }
1090   else {
1091     *badPtr = val;
1092     return 0;
1093   }
1094   while (isSpace(toAscii(enc, ptr, end)))
1095     ptr += enc->minBytesPerChar;
1096   if (ptr != end) {
1097     *badPtr = ptr;
1098     return 0;
1099   }
1100   return 1;
1101 }
1102
1103 static
1104 int checkCharRefNumber(int result)
1105 {
1106   switch (result >> 8) {
1107   case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1108   case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1109     return -1;
1110   case 0:
1111     if (latin1_encoding.type[result] == BT_NONXML)
1112       return -1;
1113     break;
1114   case 0xFF:
1115     if (result == 0xFFFE || result == 0xFFFF)
1116       return -1;
1117     break;
1118   }
1119   return result;
1120 }
1121
1122 int xmlrpc_XmlUtf8Encode(int c, char *buf)
1123 {
1124   enum {
1125     /* minN is minimum legal resulting value for N byte sequence */
1126     min2 = 0x80,
1127     min3 = 0x800,
1128     min4 = 0x10000
1129   };
1130
1131   if (c < 0)
1132     return 0;
1133   if (c < min2) {
1134     buf[0] = (c | UTF8_cval1);
1135     return 1;
1136   }
1137   if (c < min3) {
1138     buf[0] = ((c >> 6) | UTF8_cval2);
1139     buf[1] = ((c & 0x3f) | 0x80);
1140     return 2;
1141   }
1142   if (c < min4) {
1143     buf[0] = ((c >> 12) | UTF8_cval3);
1144     buf[1] = (((c >> 6) & 0x3f) | 0x80);
1145     buf[2] = ((c & 0x3f) | 0x80);
1146     return 3;
1147   }
1148   if (c < 0x110000) {
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);
1153     return 4;
1154   }
1155   return 0;
1156 }
1157
1158 int xmlrpc_XmlUtf16Encode(int charNum, unsigned short *buf)
1159 {
1160   if (charNum < 0)
1161     return 0;
1162   if (charNum < 0x10000) {
1163     buf[0] = charNum;
1164     return 1;
1165   }
1166   if (charNum < 0x110000) {
1167     charNum -= 0x10000;
1168     buf[0] = (charNum >> 10) + 0xD800;
1169     buf[1] = (charNum & 0x3FF) + 0xDC00;
1170     return 2;
1171   }
1172   return 0;
1173 }
1174
1175 struct unknown_encoding {
1176   struct normal_encoding normal;
1177   int (*convert)(void *userData, const char *p);
1178   void *userData;
1179   unsigned short utf16[256];
1180   char utf8[256][4];
1181 };
1182
1183 int xmlrpc_XmlSizeOfUnknownEncoding(void)
1184 {
1185   return sizeof(struct unknown_encoding);
1186 }
1187
1188 static
1189 int unknown_isName(const ENCODING *enc, const char *p)
1190 {
1191   int c = ((const struct unknown_encoding *)enc)
1192           ->convert(((const struct unknown_encoding *)enc)->userData, p);
1193   if (c & ~0xFFFF)
1194     return 0;
1195   return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
1196 }
1197
1198 static
1199 int unknown_isNmstrt(const ENCODING *enc, const char *p)
1200 {
1201   int c = ((const struct unknown_encoding *)enc)
1202           ->convert(((const struct unknown_encoding *)enc)->userData, p);
1203   if (c & ~0xFFFF)
1204     return 0;
1205   return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
1206 }
1207
1208 static
1209 int unknown_isInvalid(const ENCODING *enc, const char *p)
1210 {
1211   int c = ((const struct unknown_encoding *)enc)
1212            ->convert(((const struct unknown_encoding *)enc)->userData, p);
1213   return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1214 }
1215
1216 static
1217 void unknown_toUtf8(const ENCODING *enc,
1218                     const char **fromP, const char *fromLim,
1219                     char **toP, const char *toLim)
1220 {
1221   char buf[XML_UTF8_ENCODE_MAX];
1222   for (;;) {
1223     const char *utf8;
1224     int n;
1225     if (*fromP == fromLim)
1226       break;
1227     utf8 = ((const struct unknown_encoding *)enc)->utf8[(unsigned char)**fromP];
1228     n = *utf8++;
1229     if (n == 0) {
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)
1234         break;
1235       utf8 = buf;
1236       *fromP += ((const struct normal_encoding *)enc)->type[(unsigned char)**fromP]
1237                  - (BT_LEAD2 - 2);
1238     }
1239     else {
1240       if (n > toLim - *toP)
1241         break;
1242       (*fromP)++;
1243     }
1244     do {
1245       *(*toP)++ = *utf8++;
1246     } while (--n != 0);
1247   }
1248 }
1249
1250 static
1251 void unknown_toUtf16(const ENCODING *enc,
1252                      const char **fromP, const char *fromLim,
1253                      unsigned short **toP, const unsigned short *toLim)
1254 {
1255   while (*fromP != fromLim && *toP != toLim) {
1256     unsigned short c
1257       = ((const struct unknown_encoding *)enc)->utf16[(unsigned char)**fromP];
1258     if (c == 0) {
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]
1262                  - (BT_LEAD2 - 2);
1263     }
1264     else
1265       (*fromP)++;
1266     *(*toP)++ = c;
1267   }
1268 }
1269
1270 ENCODING *
1271 xmlrpc_XmlInitUnknownEncoding(void *mem,
1272                        int *table,
1273                        int (*convert)(void *userData, const char *p),
1274                        void *userData)
1275 {
1276   int i;
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
1283         && table[i] != i)
1284       return 0;
1285   for (i = 0; i < 256; i++) {
1286     int c = table[i];
1287     if (c == -1) {
1288       e->normal.type[i] = BT_MALFORM;
1289       /* This shouldn't really get used. */
1290       e->utf16[i] = 0xFFFF;
1291       e->utf8[i][0] = 1;
1292       e->utf8[i][1] = 0;
1293     }
1294     else if (c < 0) {
1295       if (c < -4)
1296         return 0;
1297       e->normal.type[i] = BT_LEAD2 - (c + 2);
1298       e->utf8[i][0] = 0;
1299       e->utf16[i] = 0;
1300     }
1301     else if (c < 0x80) {
1302       if (latin1_encoding.type[c] != BT_OTHER
1303           && latin1_encoding.type[c] != BT_NONXML
1304           && c != i)
1305         return 0;
1306       e->normal.type[i] = latin1_encoding.type[c];
1307       e->utf8[i][0] = 1;
1308       e->utf8[i][1] = (char)c;
1309       e->utf16[i] = c == 0 ? 0xFFFF : c;
1310     }
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;
1315       e->utf8[i][0] = 1;
1316       e->utf8[i][1] = 0;
1317     }
1318     else {
1319       if (c > 0xFFFF)
1320         return 0;
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;
1325       else
1326         e->normal.type[i] = BT_OTHER;
1327       e->utf8[i][0] = (char)xmlrpc_XmlUtf8Encode(c, e->utf8[i] + 1);
1328       e->utf16[i] = c;
1329     }
1330   }
1331   e->userData = userData;
1332   e->convert = convert;
1333   if (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;
1343   }
1344   e->normal.enc.utf8Convert = unknown_toUtf8;
1345   e->normal.enc.utf16Convert = unknown_toUtf16;
1346   return &(e->normal.enc);
1347 }
1348
1349 /* If this enumeration is changed, getEncodingIndex and encodings
1350 must also be changed. */
1351 enum {
1352   UNKNOWN_ENC = -1,
1353   ISO_8859_1_ENC = 0,
1354   US_ASCII_ENC,
1355   UTF_8_ENC,
1356   UTF_16_ENC,
1357   UTF_16BE_ENC,
1358   UTF_16LE_ENC,
1359   /* must match encodingNames up to here */
1360   NO_ENC
1361 };
1362
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'
1365 };
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'
1368 };
1369 static const char KW_UTF_8[] =  {
1370   ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
1371 };
1372 static const char KW_UTF_16[] = {
1373   ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
1374 };
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'
1377 };
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'
1380 };
1381
1382 static
1383 int getEncodingIndex(const char *name)
1384 {
1385   static const char *encodingNames[] = {
1386     KW_ISO_8859_1,
1387     KW_US_ASCII,
1388     KW_UTF_8,
1389     KW_UTF_16,
1390     KW_UTF_16BE,
1391     KW_UTF_16LE,
1392   };
1393   int i;
1394   if (name == 0)
1395     return NO_ENC;
1396   for (i = 0; i < (int)(sizeof(encodingNames)/sizeof(encodingNames[0])); i++)
1397     if (streqci(name, encodingNames[i]))
1398       return i;
1399   return UNKNOWN_ENC;
1400 }
1401
1402 /* For binary compatibility, we store the index of the encoding specified
1403 at initialization in the isUtf16 member. */
1404
1405 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1406 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1407
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.
1413 */
1414
1415
1416 static
1417 int initScan(const ENCODING **encodingTable,
1418              const INIT_ENCODING *enc,
1419              int state,
1420              const char *ptr,
1421              const char *end,
1422              const char **nextTokPtr)
1423 {
1424   const ENCODING **encPtr;
1425
1426   if (ptr == end)
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;
1435 #endif
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)) {
1439     case UTF_16_ENC:
1440     case UTF_16LE_ENC:
1441     case UTF_16BE_ENC:
1442       return XML_TOK_PARTIAL;
1443     }
1444     switch ((unsigned char)*ptr) {
1445     case 0xFE:
1446     case 0xFF:
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)
1450         break;
1451       /* fall through */
1452     case 0x00:
1453     case 0x3C:
1454       return XML_TOK_PARTIAL;
1455     }
1456   }
1457   else {
1458     switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
1459     case 0xFEFF:
1460       if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1461           && state == XML_CONTENT_STATE)
1462         break;
1463       *nextTokPtr = ptr + 2;
1464       *encPtr = encodingTable[UTF_16BE_ENC];
1465       return XML_TOK_BOM;
1466     /* 00 3C is handled in the default case */
1467     case 0x3C00:
1468       if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
1469            || INIT_ENC_INDEX(enc) == UTF_16_ENC)
1470           && state == XML_CONTENT_STATE)
1471         break;
1472       *encPtr = encodingTable[UTF_16LE_ENC];
1473       return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1474     case 0xFFFE:
1475       if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1476           && state == XML_CONTENT_STATE)
1477         break;
1478       *nextTokPtr = ptr + 2;
1479       *encPtr = encodingTable[UTF_16LE_ENC];
1480       return XML_TOK_BOM;
1481     case 0xEFBB:
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)
1491           break;
1492       }
1493       if (ptr + 2 == end)
1494         return XML_TOK_PARTIAL;
1495       if ((unsigned char)ptr[2] == 0xBF) {
1496         *encPtr = encodingTable[UTF_8_ENC];
1497         return XML_TOK_BOM;
1498       }
1499       break;
1500     default:
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
1505            UTF-16LE. */
1506         if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
1507           break;
1508         *encPtr = encodingTable[UTF_16BE_ENC];
1509         return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1510       }
1511       else if (ptr[1] == '\0') {
1512         /* We could recover here in the case:
1513             - parsing an external entity
1514             - second byte is 0
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)
1521           break;
1522         *encPtr = encodingTable[UTF_16LE_ENC];
1523         return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1524       }
1525       break;
1526     }
1527   }
1528   *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1529   return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1530 }
1531
1532
1533 #define NS(x) x
1534 #define ns(x) x
1535 #include "xmltok_ns.c"
1536 #undef NS
1537 #undef ns
1538
1539 #ifdef XML_NS
1540
1541 #define NS(x) x ## NS
1542 #define ns(x) x ## _ns
1543
1544 #include "xmltok_ns.c"
1545
1546 #undef NS
1547 #undef ns
1548
1549 ENCODING *
1550 xmlrpc_XmlInitUnknownEncodingNS(void *mem,
1551                          int *table,
1552                          int (*convert)(void *userData, const char *p),
1553                          void *userData)
1554 {
1555   ENCODING *enc = xmlrpc_XmlInitUnknownEncoding(mem, table, convert, userData);
1556   if (enc)
1557     ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;
1558   return enc;
1559 }
1560
1561 #endif /* XML_NS */