xref: /freebsd/contrib/expat/lib/xmltok.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /* Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
2    See the file COPYING for copying permission.
3 */
4 
5 #include <stddef.h>
6 
7 #ifdef WIN32
8 #include "winconfig.h"
9 #elif defined(MACOS_CLASSIC)
10 #include "macconfig.h"
11 #elif defined(__amigaos__)
12 #include "amigaconfig.h"
13 #elif defined(__WATCOMC__)
14 #include "watcomconfig.h"
15 #else
16 #ifdef HAVE_EXPAT_CONFIG_H
17 #include <expat_config.h>
18 #endif
19 #endif /* ndef WIN32 */
20 
21 #include "expat_external.h"
22 #include "internal.h"
23 #include "xmltok.h"
24 #include "nametab.h"
25 
26 #ifdef XML_DTD
27 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
28 #else
29 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
30 #endif
31 
32 #define VTABLE1 \
33   { PREFIX(prologTok), PREFIX(contentTok), \
34     PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
35   { PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
36   PREFIX(sameName), \
37   PREFIX(nameMatchesAscii), \
38   PREFIX(nameLength), \
39   PREFIX(skipS), \
40   PREFIX(getAtts), \
41   PREFIX(charRefNumber), \
42   PREFIX(predefinedEntityName), \
43   PREFIX(updatePosition), \
44   PREFIX(isPublicId)
45 
46 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
47 
48 #define UCS2_GET_NAMING(pages, hi, lo) \
49    (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1u << ((lo) & 0x1F)))
50 
51 /* A 2 byte UTF-8 representation splits the characters 11 bits between
52    the bottom 5 and 6 bits of the bytes.  We need 8 bits to index into
53    pages, 3 bits to add to that index and 5 bits to generate the mask.
54 */
55 #define UTF8_GET_NAMING2(pages, byte) \
56     (namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
57                       + ((((byte)[0]) & 3) << 1) \
58                       + ((((byte)[1]) >> 5) & 1)] \
59          & (1u << (((byte)[1]) & 0x1F)))
60 
61 /* A 3 byte UTF-8 representation splits the characters 16 bits between
62    the bottom 4, 6 and 6 bits of the bytes.  We need 8 bits to index
63    into pages, 3 bits to add to that index and 5 bits to generate the
64    mask.
65 */
66 #define UTF8_GET_NAMING3(pages, byte) \
67   (namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
68                              + ((((byte)[1]) >> 2) & 0xF)] \
69                        << 3) \
70                       + ((((byte)[1]) & 3) << 1) \
71                       + ((((byte)[2]) >> 5) & 1)] \
72          & (1u << (((byte)[2]) & 0x1F)))
73 
74 #define UTF8_GET_NAMING(pages, p, n) \
75   ((n) == 2 \
76   ? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
77   : ((n) == 3 \
78      ? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
79      : 0))
80 
81 /* Detection of invalid UTF-8 sequences is based on Table 3.1B
82    of Unicode 3.2: http://www.unicode.org/unicode/reports/tr28/
83    with the additional restriction of not allowing the Unicode
84    code points 0xFFFF and 0xFFFE (sequences EF,BF,BF and EF,BF,BE).
85    Implementation details:
86      (A & 0x80) == 0     means A < 0x80
87    and
88      (A & 0xC0) == 0xC0  means A > 0xBF
89 */
90 
91 #define UTF8_INVALID2(p) \
92   ((*p) < 0xC2 || ((p)[1] & 0x80) == 0 || ((p)[1] & 0xC0) == 0xC0)
93 
94 #define UTF8_INVALID3(p) \
95   (((p)[2] & 0x80) == 0 \
96   || \
97   ((*p) == 0xEF && (p)[1] == 0xBF \
98     ? \
99     (p)[2] > 0xBD \
100     : \
101     ((p)[2] & 0xC0) == 0xC0) \
102   || \
103   ((*p) == 0xE0 \
104     ? \
105     (p)[1] < 0xA0 || ((p)[1] & 0xC0) == 0xC0 \
106     : \
107     ((p)[1] & 0x80) == 0 \
108     || \
109     ((*p) == 0xED ? (p)[1] > 0x9F : ((p)[1] & 0xC0) == 0xC0)))
110 
111 #define UTF8_INVALID4(p) \
112   (((p)[3] & 0x80) == 0 || ((p)[3] & 0xC0) == 0xC0 \
113   || \
114   ((p)[2] & 0x80) == 0 || ((p)[2] & 0xC0) == 0xC0 \
115   || \
116   ((*p) == 0xF0 \
117     ? \
118     (p)[1] < 0x90 || ((p)[1] & 0xC0) == 0xC0 \
119     : \
120     ((p)[1] & 0x80) == 0 \
121     || \
122     ((*p) == 0xF4 ? (p)[1] > 0x8F : ((p)[1] & 0xC0) == 0xC0)))
123 
124 static int PTRFASTCALL
125 isNever(const ENCODING *UNUSED_P(enc), const char *UNUSED_P(p))
126 {
127   return 0;
128 }
129 
130 static int PTRFASTCALL
131 utf8_isName2(const ENCODING *UNUSED_P(enc), const char *p)
132 {
133   return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
134 }
135 
136 static int PTRFASTCALL
137 utf8_isName3(const ENCODING *UNUSED_P(enc), const char *p)
138 {
139   return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
140 }
141 
142 #define utf8_isName4 isNever
143 
144 static int PTRFASTCALL
145 utf8_isNmstrt2(const ENCODING *UNUSED_P(enc), const char *p)
146 {
147   return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
148 }
149 
150 static int PTRFASTCALL
151 utf8_isNmstrt3(const ENCODING *UNUSED_P(enc), const char *p)
152 {
153   return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
154 }
155 
156 #define utf8_isNmstrt4 isNever
157 
158 static int PTRFASTCALL
159 utf8_isInvalid2(const ENCODING *UNUSED_P(enc), const char *p)
160 {
161   return UTF8_INVALID2((const unsigned char *)p);
162 }
163 
164 static int PTRFASTCALL
165 utf8_isInvalid3(const ENCODING *UNUSED_P(enc), const char *p)
166 {
167   return UTF8_INVALID3((const unsigned char *)p);
168 }
169 
170 static int PTRFASTCALL
171 utf8_isInvalid4(const ENCODING *UNUSED_P(enc), const char *p)
172 {
173   return UTF8_INVALID4((const unsigned char *)p);
174 }
175 
176 struct normal_encoding {
177   ENCODING enc;
178   unsigned char type[256];
179 #ifdef XML_MIN_SIZE
180   int (PTRFASTCALL *byteType)(const ENCODING *, const char *);
181   int (PTRFASTCALL *isNameMin)(const ENCODING *, const char *);
182   int (PTRFASTCALL *isNmstrtMin)(const ENCODING *, const char *);
183   int (PTRFASTCALL *byteToAscii)(const ENCODING *, const char *);
184   int (PTRCALL *charMatches)(const ENCODING *, const char *, int);
185 #endif /* XML_MIN_SIZE */
186   int (PTRFASTCALL *isName2)(const ENCODING *, const char *);
187   int (PTRFASTCALL *isName3)(const ENCODING *, const char *);
188   int (PTRFASTCALL *isName4)(const ENCODING *, const char *);
189   int (PTRFASTCALL *isNmstrt2)(const ENCODING *, const char *);
190   int (PTRFASTCALL *isNmstrt3)(const ENCODING *, const char *);
191   int (PTRFASTCALL *isNmstrt4)(const ENCODING *, const char *);
192   int (PTRFASTCALL *isInvalid2)(const ENCODING *, const char *);
193   int (PTRFASTCALL *isInvalid3)(const ENCODING *, const char *);
194   int (PTRFASTCALL *isInvalid4)(const ENCODING *, const char *);
195 };
196 
197 #define AS_NORMAL_ENCODING(enc)   ((const struct normal_encoding *) (enc))
198 
199 #ifdef XML_MIN_SIZE
200 
201 #define STANDARD_VTABLE(E) \
202  E ## byteType, \
203  E ## isNameMin, \
204  E ## isNmstrtMin, \
205  E ## byteToAscii, \
206  E ## charMatches,
207 
208 #else
209 
210 #define STANDARD_VTABLE(E) /* as nothing */
211 
212 #endif
213 
214 #define NORMAL_VTABLE(E) \
215  E ## isName2, \
216  E ## isName3, \
217  E ## isName4, \
218  E ## isNmstrt2, \
219  E ## isNmstrt3, \
220  E ## isNmstrt4, \
221  E ## isInvalid2, \
222  E ## isInvalid3, \
223  E ## isInvalid4
224 
225 #define NULL_VTABLE \
226  /* isName2 */ NULL, \
227  /* isName3 */ NULL, \
228  /* isName4 */ NULL, \
229  /* isNmstrt2 */ NULL, \
230  /* isNmstrt3 */ NULL, \
231  /* isNmstrt4 */ NULL, \
232  /* isInvalid2 */ NULL, \
233  /* isInvalid3 */ NULL, \
234  /* isInvalid4 */ NULL
235 
236 static int FASTCALL checkCharRefNumber(int);
237 
238 #include "xmltok_impl.h"
239 #include "ascii.h"
240 
241 #ifdef XML_MIN_SIZE
242 #define sb_isNameMin isNever
243 #define sb_isNmstrtMin isNever
244 #endif
245 
246 #ifdef XML_MIN_SIZE
247 #define MINBPC(enc) ((enc)->minBytesPerChar)
248 #else
249 /* minimum bytes per character */
250 #define MINBPC(enc) 1
251 #endif
252 
253 #define SB_BYTE_TYPE(enc, p) \
254   (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
255 
256 #ifdef XML_MIN_SIZE
257 static int PTRFASTCALL
258 sb_byteType(const ENCODING *enc, const char *p)
259 {
260   return SB_BYTE_TYPE(enc, p);
261 }
262 #define BYTE_TYPE(enc, p) \
263  (AS_NORMAL_ENCODING(enc)->byteType(enc, p))
264 #else
265 #define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
266 #endif
267 
268 #ifdef XML_MIN_SIZE
269 #define BYTE_TO_ASCII(enc, p) \
270  (AS_NORMAL_ENCODING(enc)->byteToAscii(enc, p))
271 static int PTRFASTCALL
272 sb_byteToAscii(const ENCODING *enc, const char *p)
273 {
274   return *p;
275 }
276 #else
277 #define BYTE_TO_ASCII(enc, p) (*(p))
278 #endif
279 
280 #define IS_NAME_CHAR(enc, p, n) \
281  (AS_NORMAL_ENCODING(enc)->isName ## n(enc, p))
282 #define IS_NMSTRT_CHAR(enc, p, n) \
283  (AS_NORMAL_ENCODING(enc)->isNmstrt ## n(enc, p))
284 #define IS_INVALID_CHAR(enc, p, n) \
285  (AS_NORMAL_ENCODING(enc)->isInvalid ## n(enc, p))
286 
287 #ifdef XML_MIN_SIZE
288 #define IS_NAME_CHAR_MINBPC(enc, p) \
289  (AS_NORMAL_ENCODING(enc)->isNameMin(enc, p))
290 #define IS_NMSTRT_CHAR_MINBPC(enc, p) \
291  (AS_NORMAL_ENCODING(enc)->isNmstrtMin(enc, p))
292 #else
293 #define IS_NAME_CHAR_MINBPC(enc, p) (0)
294 #define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
295 #endif
296 
297 #ifdef XML_MIN_SIZE
298 #define CHAR_MATCHES(enc, p, c) \
299  (AS_NORMAL_ENCODING(enc)->charMatches(enc, p, c))
300 static int PTRCALL
301 sb_charMatches(const ENCODING *enc, const char *p, int c)
302 {
303   return *p == c;
304 }
305 #else
306 /* c is an ASCII character */
307 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
308 #endif
309 
310 #define PREFIX(ident) normal_ ## ident
311 #define XML_TOK_IMPL_C
312 #include "xmltok_impl.c"
313 #undef XML_TOK_IMPL_C
314 
315 #undef MINBPC
316 #undef BYTE_TYPE
317 #undef BYTE_TO_ASCII
318 #undef CHAR_MATCHES
319 #undef IS_NAME_CHAR
320 #undef IS_NAME_CHAR_MINBPC
321 #undef IS_NMSTRT_CHAR
322 #undef IS_NMSTRT_CHAR_MINBPC
323 #undef IS_INVALID_CHAR
324 
325 enum {  /* UTF8_cvalN is value of masked first byte of N byte sequence */
326   UTF8_cval1 = 0x00,
327   UTF8_cval2 = 0xc0,
328   UTF8_cval3 = 0xe0,
329   UTF8_cval4 = 0xf0
330 };
331 
332 void
333 align_limit_to_full_utf8_characters(const char * from, const char ** fromLimRef)
334 {
335   const char * fromLim = *fromLimRef;
336   size_t walked = 0;
337   for (; fromLim > from; fromLim--, walked++) {
338     const unsigned char prev = (unsigned char)fromLim[-1];
339     if ((prev & 0xf8u) == 0xf0u) { /* 4-byte character, lead by 0b11110xxx byte */
340       if (walked + 1 >= 4) {
341         fromLim += 4 - 1;
342         break;
343       } else {
344         walked = 0;
345       }
346     } else if ((prev & 0xf0u) == 0xe0u) { /* 3-byte character, lead by 0b1110xxxx byte */
347       if (walked + 1 >= 3) {
348         fromLim += 3 - 1;
349         break;
350       } else {
351         walked = 0;
352       }
353     } else if ((prev & 0xe0u) == 0xc0u) { /* 2-byte character, lead by 0b110xxxxx byte */
354       if (walked + 1 >= 2) {
355         fromLim += 2 - 1;
356         break;
357       } else {
358         walked = 0;
359       }
360     } else if ((prev & 0x80u) == 0x00u) { /* 1-byte character, matching 0b0xxxxxxx */
361       break;
362     }
363   }
364   *fromLimRef = fromLim;
365 }
366 
367 static enum XML_Convert_Result PTRCALL
368 utf8_toUtf8(const ENCODING *UNUSED_P(enc),
369             const char **fromP, const char *fromLim,
370             char **toP, const char *toLim)
371 {
372   enum XML_Convert_Result res = XML_CONVERT_COMPLETED;
373   char *to;
374   const char *from;
375   if (fromLim - *fromP > toLim - *toP) {
376     /* Avoid copying partial characters. */
377     res = XML_CONVERT_OUTPUT_EXHAUSTED;
378     fromLim = *fromP + (toLim - *toP);
379     align_limit_to_full_utf8_characters(*fromP, &fromLim);
380   }
381   for (to = *toP, from = *fromP; (from < fromLim) && (to < toLim); from++, to++)
382     *to = *from;
383   *fromP = from;
384   *toP = to;
385 
386   if ((to == toLim) && (from < fromLim))
387     return XML_CONVERT_OUTPUT_EXHAUSTED;
388   else
389     return res;
390 }
391 
392 static enum XML_Convert_Result PTRCALL
393 utf8_toUtf16(const ENCODING *enc,
394              const char **fromP, const char *fromLim,
395              unsigned short **toP, const unsigned short *toLim)
396 {
397   enum XML_Convert_Result res = XML_CONVERT_COMPLETED;
398   unsigned short *to = *toP;
399   const char *from = *fromP;
400   while (from < fromLim && to < toLim) {
401     switch (((struct normal_encoding *)enc)->type[(unsigned char)*from]) {
402     case BT_LEAD2:
403       if (fromLim - from < 2) {
404         res = XML_CONVERT_INPUT_INCOMPLETE;
405         break;
406       }
407       *to++ = (unsigned short)(((from[0] & 0x1f) << 6) | (from[1] & 0x3f));
408       from += 2;
409       break;
410     case BT_LEAD3:
411       if (fromLim - from < 3) {
412         res = XML_CONVERT_INPUT_INCOMPLETE;
413         break;
414       }
415       *to++ = (unsigned short)(((from[0] & 0xf) << 12)
416                                | ((from[1] & 0x3f) << 6) | (from[2] & 0x3f));
417       from += 3;
418       break;
419     case BT_LEAD4:
420       {
421         unsigned long n;
422         if (toLim - to < 2) {
423           res = XML_CONVERT_OUTPUT_EXHAUSTED;
424           goto after;
425         }
426         if (fromLim - from < 4) {
427           res = XML_CONVERT_INPUT_INCOMPLETE;
428           goto after;
429         }
430         n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12)
431             | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
432         n -= 0x10000;
433         to[0] = (unsigned short)((n >> 10) | 0xD800);
434         to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
435         to += 2;
436         from += 4;
437       }
438       break;
439     default:
440       *to++ = *from++;
441       break;
442     }
443   }
444 after:
445   *fromP = from;
446   *toP = to;
447   return res;
448 }
449 
450 #ifdef XML_NS
451 static const struct normal_encoding utf8_encoding_ns = {
452   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
453   {
454 #include "asciitab.h"
455 #include "utf8tab.h"
456   },
457   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
458 };
459 #endif
460 
461 static const struct normal_encoding utf8_encoding = {
462   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
463   {
464 #define BT_COLON BT_NMSTRT
465 #include "asciitab.h"
466 #undef BT_COLON
467 #include "utf8tab.h"
468   },
469   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
470 };
471 
472 #ifdef XML_NS
473 
474 static const struct normal_encoding internal_utf8_encoding_ns = {
475   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
476   {
477 #include "iasciitab.h"
478 #include "utf8tab.h"
479   },
480   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
481 };
482 
483 #endif
484 
485 static const struct normal_encoding internal_utf8_encoding = {
486   { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
487   {
488 #define BT_COLON BT_NMSTRT
489 #include "iasciitab.h"
490 #undef BT_COLON
491 #include "utf8tab.h"
492   },
493   STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
494 };
495 
496 static enum XML_Convert_Result PTRCALL
497 latin1_toUtf8(const ENCODING *UNUSED_P(enc),
498               const char **fromP, const char *fromLim,
499               char **toP, const char *toLim)
500 {
501   for (;;) {
502     unsigned char c;
503     if (*fromP == fromLim)
504       return XML_CONVERT_COMPLETED;
505     c = (unsigned char)**fromP;
506     if (c & 0x80) {
507       if (toLim - *toP < 2)
508         return XML_CONVERT_OUTPUT_EXHAUSTED;
509       *(*toP)++ = (char)((c >> 6) | UTF8_cval2);
510       *(*toP)++ = (char)((c & 0x3f) | 0x80);
511       (*fromP)++;
512     }
513     else {
514       if (*toP == toLim)
515         return XML_CONVERT_OUTPUT_EXHAUSTED;
516       *(*toP)++ = *(*fromP)++;
517     }
518   }
519 }
520 
521 static enum XML_Convert_Result PTRCALL
522 latin1_toUtf16(const ENCODING *UNUSED_P(enc),
523                const char **fromP, const char *fromLim,
524                unsigned short **toP, const unsigned short *toLim)
525 {
526   while (*fromP < fromLim && *toP < toLim)
527     *(*toP)++ = (unsigned char)*(*fromP)++;
528 
529   if ((*toP == toLim) && (*fromP < fromLim))
530     return XML_CONVERT_OUTPUT_EXHAUSTED;
531   else
532     return XML_CONVERT_COMPLETED;
533 }
534 
535 #ifdef XML_NS
536 
537 static const struct normal_encoding latin1_encoding_ns = {
538   { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
539   {
540 #include "asciitab.h"
541 #include "latin1tab.h"
542   },
543   STANDARD_VTABLE(sb_) NULL_VTABLE
544 };
545 
546 #endif
547 
548 static const struct normal_encoding latin1_encoding = {
549   { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
550   {
551 #define BT_COLON BT_NMSTRT
552 #include "asciitab.h"
553 #undef BT_COLON
554 #include "latin1tab.h"
555   },
556   STANDARD_VTABLE(sb_) NULL_VTABLE
557 };
558 
559 static enum XML_Convert_Result PTRCALL
560 ascii_toUtf8(const ENCODING *UNUSED_P(enc),
561              const char **fromP, const char *fromLim,
562              char **toP, const char *toLim)
563 {
564   while (*fromP < fromLim && *toP < toLim)
565     *(*toP)++ = *(*fromP)++;
566 
567   if ((*toP == toLim) && (*fromP < fromLim))
568     return XML_CONVERT_OUTPUT_EXHAUSTED;
569   else
570     return XML_CONVERT_COMPLETED;
571 }
572 
573 #ifdef XML_NS
574 
575 static const struct normal_encoding ascii_encoding_ns = {
576   { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
577   {
578 #include "asciitab.h"
579 /* BT_NONXML == 0 */
580   },
581   STANDARD_VTABLE(sb_) NULL_VTABLE
582 };
583 
584 #endif
585 
586 static const struct normal_encoding ascii_encoding = {
587   { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
588   {
589 #define BT_COLON BT_NMSTRT
590 #include "asciitab.h"
591 #undef BT_COLON
592 /* BT_NONXML == 0 */
593   },
594   STANDARD_VTABLE(sb_) NULL_VTABLE
595 };
596 
597 static int PTRFASTCALL
598 unicode_byte_type(char hi, char lo)
599 {
600   switch ((unsigned char)hi) {
601   case 0xD8: case 0xD9: case 0xDA: case 0xDB:
602     return BT_LEAD4;
603   case 0xDC: case 0xDD: case 0xDE: case 0xDF:
604     return BT_TRAIL;
605   case 0xFF:
606     switch ((unsigned char)lo) {
607     case 0xFF:
608     case 0xFE:
609       return BT_NONXML;
610     }
611     break;
612   }
613   return BT_NONASCII;
614 }
615 
616 #define DEFINE_UTF16_TO_UTF8(E) \
617 static enum XML_Convert_Result  PTRCALL \
618 E ## toUtf8(const ENCODING *UNUSED_P(enc), \
619             const char **fromP, const char *fromLim, \
620             char **toP, const char *toLim) \
621 { \
622   const char *from = *fromP; \
623   fromLim = from + (((fromLim - from) >> 1) << 1);  /* shrink to even */ \
624   for (; from < fromLim; from += 2) { \
625     int plane; \
626     unsigned char lo2; \
627     unsigned char lo = GET_LO(from); \
628     unsigned char hi = GET_HI(from); \
629     switch (hi) { \
630     case 0: \
631       if (lo < 0x80) { \
632         if (*toP == toLim) { \
633           *fromP = from; \
634           return XML_CONVERT_OUTPUT_EXHAUSTED; \
635         } \
636         *(*toP)++ = lo; \
637         break; \
638       } \
639       /* fall through */ \
640     case 0x1: case 0x2: case 0x3: \
641     case 0x4: case 0x5: case 0x6: case 0x7: \
642       if (toLim -  *toP < 2) { \
643         *fromP = from; \
644         return XML_CONVERT_OUTPUT_EXHAUSTED; \
645       } \
646       *(*toP)++ = ((lo >> 6) | (hi << 2) |  UTF8_cval2); \
647       *(*toP)++ = ((lo & 0x3f) | 0x80); \
648       break; \
649     default: \
650       if (toLim -  *toP < 3)  { \
651         *fromP = from; \
652         return XML_CONVERT_OUTPUT_EXHAUSTED; \
653       } \
654       /* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
655       *(*toP)++ = ((hi >> 4) | UTF8_cval3); \
656       *(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
657       *(*toP)++ = ((lo & 0x3f) | 0x80); \
658       break; \
659     case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
660       if (toLim -  *toP < 4) { \
661         *fromP = from; \
662         return XML_CONVERT_OUTPUT_EXHAUSTED; \
663       } \
664       if (fromLim - from < 4) { \
665         *fromP = from; \
666         return XML_CONVERT_INPUT_INCOMPLETE; \
667       } \
668       plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
669       *(*toP)++ = ((plane >> 2) | UTF8_cval4); \
670       *(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
671       from += 2; \
672       lo2 = GET_LO(from); \
673       *(*toP)++ = (((lo & 0x3) << 4) \
674                    | ((GET_HI(from) & 0x3) << 2) \
675                    | (lo2 >> 6) \
676                    | 0x80); \
677       *(*toP)++ = ((lo2 & 0x3f) | 0x80); \
678       break; \
679     } \
680   } \
681   *fromP = from; \
682   if (from < fromLim) \
683     return XML_CONVERT_INPUT_INCOMPLETE; \
684   else \
685     return XML_CONVERT_COMPLETED; \
686 }
687 
688 #define DEFINE_UTF16_TO_UTF16(E) \
689 static enum XML_Convert_Result  PTRCALL \
690 E ## toUtf16(const ENCODING *UNUSED_P(enc), \
691              const char **fromP, const char *fromLim, \
692              unsigned short **toP, const unsigned short *toLim) \
693 { \
694   enum XML_Convert_Result res = XML_CONVERT_COMPLETED; \
695   fromLim = *fromP + (((fromLim - *fromP) >> 1) << 1);  /* shrink to even */ \
696   /* Avoid copying first half only of surrogate */ \
697   if (fromLim - *fromP > ((toLim - *toP) << 1) \
698       && (GET_HI(fromLim - 2) & 0xF8) == 0xD8) { \
699     fromLim -= 2; \
700     res = XML_CONVERT_INPUT_INCOMPLETE; \
701   } \
702   for (; *fromP < fromLim && *toP < toLim; *fromP += 2) \
703     *(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
704   if ((*toP == toLim) && (*fromP < fromLim)) \
705     return XML_CONVERT_OUTPUT_EXHAUSTED; \
706   else \
707     return res; \
708 }
709 
710 #define SET2(ptr, ch) \
711   (((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
712 #define GET_LO(ptr) ((unsigned char)(ptr)[0])
713 #define GET_HI(ptr) ((unsigned char)(ptr)[1])
714 
715 DEFINE_UTF16_TO_UTF8(little2_)
716 DEFINE_UTF16_TO_UTF16(little2_)
717 
718 #undef SET2
719 #undef GET_LO
720 #undef GET_HI
721 
722 #define SET2(ptr, ch) \
723   (((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
724 #define GET_LO(ptr) ((unsigned char)(ptr)[1])
725 #define GET_HI(ptr) ((unsigned char)(ptr)[0])
726 
727 DEFINE_UTF16_TO_UTF8(big2_)
728 DEFINE_UTF16_TO_UTF16(big2_)
729 
730 #undef SET2
731 #undef GET_LO
732 #undef GET_HI
733 
734 #define LITTLE2_BYTE_TYPE(enc, p) \
735  ((p)[1] == 0 \
736   ? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
737   : unicode_byte_type((p)[1], (p)[0]))
738 #define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
739 #define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
740 #define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
741   UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
742 #define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
743   UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
744 
745 #ifdef XML_MIN_SIZE
746 
747 static int PTRFASTCALL
748 little2_byteType(const ENCODING *enc, const char *p)
749 {
750   return LITTLE2_BYTE_TYPE(enc, p);
751 }
752 
753 static int PTRFASTCALL
754 little2_byteToAscii(const ENCODING *enc, const char *p)
755 {
756   return LITTLE2_BYTE_TO_ASCII(enc, p);
757 }
758 
759 static int PTRCALL
760 little2_charMatches(const ENCODING *enc, const char *p, int c)
761 {
762   return LITTLE2_CHAR_MATCHES(enc, p, c);
763 }
764 
765 static int PTRFASTCALL
766 little2_isNameMin(const ENCODING *enc, const char *p)
767 {
768   return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
769 }
770 
771 static int PTRFASTCALL
772 little2_isNmstrtMin(const ENCODING *enc, const char *p)
773 {
774   return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
775 }
776 
777 #undef VTABLE
778 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
779 
780 #else /* not XML_MIN_SIZE */
781 
782 #undef PREFIX
783 #define PREFIX(ident) little2_ ## ident
784 #define MINBPC(enc) 2
785 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
786 #define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
787 #define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
788 #define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
789 #define IS_NAME_CHAR(enc, p, n) 0
790 #define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
791 #define IS_NMSTRT_CHAR(enc, p, n) (0)
792 #define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
793 
794 #define XML_TOK_IMPL_C
795 #include "xmltok_impl.c"
796 #undef XML_TOK_IMPL_C
797 
798 #undef MINBPC
799 #undef BYTE_TYPE
800 #undef BYTE_TO_ASCII
801 #undef CHAR_MATCHES
802 #undef IS_NAME_CHAR
803 #undef IS_NAME_CHAR_MINBPC
804 #undef IS_NMSTRT_CHAR
805 #undef IS_NMSTRT_CHAR_MINBPC
806 #undef IS_INVALID_CHAR
807 
808 #endif /* not XML_MIN_SIZE */
809 
810 #ifdef XML_NS
811 
812 static const struct normal_encoding little2_encoding_ns = {
813   { VTABLE, 2, 0,
814 #if BYTEORDER == 1234
815     1
816 #else
817     0
818 #endif
819   },
820   {
821 #include "asciitab.h"
822 #include "latin1tab.h"
823   },
824   STANDARD_VTABLE(little2_) NULL_VTABLE
825 };
826 
827 #endif
828 
829 static const struct normal_encoding little2_encoding = {
830   { VTABLE, 2, 0,
831 #if BYTEORDER == 1234
832     1
833 #else
834     0
835 #endif
836   },
837   {
838 #define BT_COLON BT_NMSTRT
839 #include "asciitab.h"
840 #undef BT_COLON
841 #include "latin1tab.h"
842   },
843   STANDARD_VTABLE(little2_) NULL_VTABLE
844 };
845 
846 #if BYTEORDER != 4321
847 
848 #ifdef XML_NS
849 
850 static const struct normal_encoding internal_little2_encoding_ns = {
851   { VTABLE, 2, 0, 1 },
852   {
853 #include "iasciitab.h"
854 #include "latin1tab.h"
855   },
856   STANDARD_VTABLE(little2_) NULL_VTABLE
857 };
858 
859 #endif
860 
861 static const struct normal_encoding internal_little2_encoding = {
862   { VTABLE, 2, 0, 1 },
863   {
864 #define BT_COLON BT_NMSTRT
865 #include "iasciitab.h"
866 #undef BT_COLON
867 #include "latin1tab.h"
868   },
869   STANDARD_VTABLE(little2_) NULL_VTABLE
870 };
871 
872 #endif
873 
874 
875 #define BIG2_BYTE_TYPE(enc, p) \
876  ((p)[0] == 0 \
877   ? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
878   : unicode_byte_type((p)[0], (p)[1]))
879 #define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
880 #define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
881 #define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
882   UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
883 #define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
884   UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
885 
886 #ifdef XML_MIN_SIZE
887 
888 static int PTRFASTCALL
889 big2_byteType(const ENCODING *enc, const char *p)
890 {
891   return BIG2_BYTE_TYPE(enc, p);
892 }
893 
894 static int PTRFASTCALL
895 big2_byteToAscii(const ENCODING *enc, const char *p)
896 {
897   return BIG2_BYTE_TO_ASCII(enc, p);
898 }
899 
900 static int PTRCALL
901 big2_charMatches(const ENCODING *enc, const char *p, int c)
902 {
903   return BIG2_CHAR_MATCHES(enc, p, c);
904 }
905 
906 static int PTRFASTCALL
907 big2_isNameMin(const ENCODING *enc, const char *p)
908 {
909   return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
910 }
911 
912 static int PTRFASTCALL
913 big2_isNmstrtMin(const ENCODING *enc, const char *p)
914 {
915   return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
916 }
917 
918 #undef VTABLE
919 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
920 
921 #else /* not XML_MIN_SIZE */
922 
923 #undef PREFIX
924 #define PREFIX(ident) big2_ ## ident
925 #define MINBPC(enc) 2
926 /* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
927 #define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
928 #define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
929 #define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
930 #define IS_NAME_CHAR(enc, p, n) 0
931 #define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
932 #define IS_NMSTRT_CHAR(enc, p, n) (0)
933 #define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
934 
935 #define XML_TOK_IMPL_C
936 #include "xmltok_impl.c"
937 #undef XML_TOK_IMPL_C
938 
939 #undef MINBPC
940 #undef BYTE_TYPE
941 #undef BYTE_TO_ASCII
942 #undef CHAR_MATCHES
943 #undef IS_NAME_CHAR
944 #undef IS_NAME_CHAR_MINBPC
945 #undef IS_NMSTRT_CHAR
946 #undef IS_NMSTRT_CHAR_MINBPC
947 #undef IS_INVALID_CHAR
948 
949 #endif /* not XML_MIN_SIZE */
950 
951 #ifdef XML_NS
952 
953 static const struct normal_encoding big2_encoding_ns = {
954   { VTABLE, 2, 0,
955 #if BYTEORDER == 4321
956   1
957 #else
958   0
959 #endif
960   },
961   {
962 #include "asciitab.h"
963 #include "latin1tab.h"
964   },
965   STANDARD_VTABLE(big2_) NULL_VTABLE
966 };
967 
968 #endif
969 
970 static const struct normal_encoding big2_encoding = {
971   { VTABLE, 2, 0,
972 #if BYTEORDER == 4321
973   1
974 #else
975   0
976 #endif
977   },
978   {
979 #define BT_COLON BT_NMSTRT
980 #include "asciitab.h"
981 #undef BT_COLON
982 #include "latin1tab.h"
983   },
984   STANDARD_VTABLE(big2_) NULL_VTABLE
985 };
986 
987 #if BYTEORDER != 1234
988 
989 #ifdef XML_NS
990 
991 static const struct normal_encoding internal_big2_encoding_ns = {
992   { VTABLE, 2, 0, 1 },
993   {
994 #include "iasciitab.h"
995 #include "latin1tab.h"
996   },
997   STANDARD_VTABLE(big2_) NULL_VTABLE
998 };
999 
1000 #endif
1001 
1002 static const struct normal_encoding internal_big2_encoding = {
1003   { VTABLE, 2, 0, 1 },
1004   {
1005 #define BT_COLON BT_NMSTRT
1006 #include "iasciitab.h"
1007 #undef BT_COLON
1008 #include "latin1tab.h"
1009   },
1010   STANDARD_VTABLE(big2_) NULL_VTABLE
1011 };
1012 
1013 #endif
1014 
1015 #undef PREFIX
1016 
1017 static int FASTCALL
1018 streqci(const char *s1, const char *s2)
1019 {
1020   for (;;) {
1021     char c1 = *s1++;
1022     char c2 = *s2++;
1023     if (ASCII_a <= c1 && c1 <= ASCII_z)
1024       c1 += ASCII_A - ASCII_a;
1025     if (ASCII_a <= c2 && c2 <= ASCII_z)
1026       c2 += ASCII_A - ASCII_a;
1027     if (c1 != c2)
1028       return 0;
1029     if (!c1)
1030       break;
1031   }
1032   return 1;
1033 }
1034 
1035 static void PTRCALL
1036 initUpdatePosition(const ENCODING *UNUSED_P(enc), const char *ptr,
1037                    const char *end, POSITION *pos)
1038 {
1039   normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
1040 }
1041 
1042 static int
1043 toAscii(const ENCODING *enc, const char *ptr, const char *end)
1044 {
1045   char buf[1];
1046   char *p = buf;
1047   XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
1048   if (p == buf)
1049     return -1;
1050   else
1051     return buf[0];
1052 }
1053 
1054 static int FASTCALL
1055 isSpace(int c)
1056 {
1057   switch (c) {
1058   case 0x20:
1059   case 0xD:
1060   case 0xA:
1061   case 0x9:
1062     return 1;
1063   }
1064   return 0;
1065 }
1066 
1067 /* Return 1 if there's just optional white space or there's an S
1068    followed by name=val.
1069 */
1070 static int
1071 parsePseudoAttribute(const ENCODING *enc,
1072                      const char *ptr,
1073                      const char *end,
1074                      const char **namePtr,
1075                      const char **nameEndPtr,
1076                      const char **valPtr,
1077                      const char **nextTokPtr)
1078 {
1079   int c;
1080   char open;
1081   if (ptr == end) {
1082     *namePtr = NULL;
1083     return 1;
1084   }
1085   if (!isSpace(toAscii(enc, ptr, end))) {
1086     *nextTokPtr = ptr;
1087     return 0;
1088   }
1089   do {
1090     ptr += enc->minBytesPerChar;
1091   } while (isSpace(toAscii(enc, ptr, end)));
1092   if (ptr == end) {
1093     *namePtr = NULL;
1094     return 1;
1095   }
1096   *namePtr = ptr;
1097   for (;;) {
1098     c = toAscii(enc, ptr, end);
1099     if (c == -1) {
1100       *nextTokPtr = ptr;
1101       return 0;
1102     }
1103     if (c == ASCII_EQUALS) {
1104       *nameEndPtr = ptr;
1105       break;
1106     }
1107     if (isSpace(c)) {
1108       *nameEndPtr = ptr;
1109       do {
1110         ptr += enc->minBytesPerChar;
1111       } while (isSpace(c = toAscii(enc, ptr, end)));
1112       if (c != ASCII_EQUALS) {
1113         *nextTokPtr = ptr;
1114         return 0;
1115       }
1116       break;
1117     }
1118     ptr += enc->minBytesPerChar;
1119   }
1120   if (ptr == *namePtr) {
1121     *nextTokPtr = ptr;
1122     return 0;
1123   }
1124   ptr += enc->minBytesPerChar;
1125   c = toAscii(enc, ptr, end);
1126   while (isSpace(c)) {
1127     ptr += enc->minBytesPerChar;
1128     c = toAscii(enc, ptr, end);
1129   }
1130   if (c != ASCII_QUOT && c != ASCII_APOS) {
1131     *nextTokPtr = ptr;
1132     return 0;
1133   }
1134   open = (char)c;
1135   ptr += enc->minBytesPerChar;
1136   *valPtr = ptr;
1137   for (;; ptr += enc->minBytesPerChar) {
1138     c = toAscii(enc, ptr, end);
1139     if (c == open)
1140       break;
1141     if (!(ASCII_a <= c && c <= ASCII_z)
1142         && !(ASCII_A <= c && c <= ASCII_Z)
1143         && !(ASCII_0 <= c && c <= ASCII_9)
1144         && c != ASCII_PERIOD
1145         && c != ASCII_MINUS
1146         && c != ASCII_UNDERSCORE) {
1147       *nextTokPtr = ptr;
1148       return 0;
1149     }
1150   }
1151   *nextTokPtr = ptr + enc->minBytesPerChar;
1152   return 1;
1153 }
1154 
1155 static const char KW_version[] = {
1156   ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
1157 };
1158 
1159 static const char KW_encoding[] = {
1160   ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
1161 };
1162 
1163 static const char KW_standalone[] = {
1164   ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a, ASCII_l, ASCII_o,
1165   ASCII_n, ASCII_e, '\0'
1166 };
1167 
1168 static const char KW_yes[] = {
1169   ASCII_y, ASCII_e, ASCII_s,  '\0'
1170 };
1171 
1172 static const char KW_no[] = {
1173   ASCII_n, ASCII_o,  '\0'
1174 };
1175 
1176 static int
1177 doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *,
1178                                                  const char *,
1179                                                  const char *),
1180                int isGeneralTextEntity,
1181                const ENCODING *enc,
1182                const char *ptr,
1183                const char *end,
1184                const char **badPtr,
1185                const char **versionPtr,
1186                const char **versionEndPtr,
1187                const char **encodingName,
1188                const ENCODING **encoding,
1189                int *standalone)
1190 {
1191   const char *val = NULL;
1192   const char *name = NULL;
1193   const char *nameEnd = NULL;
1194   ptr += 5 * enc->minBytesPerChar;
1195   end -= 2 * enc->minBytesPerChar;
1196   if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)
1197       || !name) {
1198     *badPtr = ptr;
1199     return 0;
1200   }
1201   if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1202     if (!isGeneralTextEntity) {
1203       *badPtr = name;
1204       return 0;
1205     }
1206   }
1207   else {
1208     if (versionPtr)
1209       *versionPtr = val;
1210     if (versionEndPtr)
1211       *versionEndPtr = ptr;
1212     if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1213       *badPtr = ptr;
1214       return 0;
1215     }
1216     if (!name) {
1217       if (isGeneralTextEntity) {
1218         /* a TextDecl must have an EncodingDecl */
1219         *badPtr = ptr;
1220         return 0;
1221       }
1222       return 1;
1223     }
1224   }
1225   if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
1226     int c = toAscii(enc, val, end);
1227     if (!(ASCII_a <= c && c <= ASCII_z) && !(ASCII_A <= c && c <= ASCII_Z)) {
1228       *badPtr = val;
1229       return 0;
1230     }
1231     if (encodingName)
1232       *encodingName = val;
1233     if (encoding)
1234       *encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
1235     if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1236       *badPtr = ptr;
1237       return 0;
1238     }
1239     if (!name)
1240       return 1;
1241   }
1242   if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone)
1243       || isGeneralTextEntity) {
1244     *badPtr = name;
1245     return 0;
1246   }
1247   if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1248     if (standalone)
1249       *standalone = 1;
1250   }
1251   else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1252     if (standalone)
1253       *standalone = 0;
1254   }
1255   else {
1256     *badPtr = val;
1257     return 0;
1258   }
1259   while (isSpace(toAscii(enc, ptr, end)))
1260     ptr += enc->minBytesPerChar;
1261   if (ptr != end) {
1262     *badPtr = ptr;
1263     return 0;
1264   }
1265   return 1;
1266 }
1267 
1268 static int FASTCALL
1269 checkCharRefNumber(int result)
1270 {
1271   switch (result >> 8) {
1272   case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1273   case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1274     return -1;
1275   case 0:
1276     if (latin1_encoding.type[result] == BT_NONXML)
1277       return -1;
1278     break;
1279   case 0xFF:
1280     if (result == 0xFFFE || result == 0xFFFF)
1281       return -1;
1282     break;
1283   }
1284   return result;
1285 }
1286 
1287 int FASTCALL
1288 XmlUtf8Encode(int c, char *buf)
1289 {
1290   enum {
1291     /* minN is minimum legal resulting value for N byte sequence */
1292     min2 = 0x80,
1293     min3 = 0x800,
1294     min4 = 0x10000
1295   };
1296 
1297   if (c < 0)
1298     return 0;
1299   if (c < min2) {
1300     buf[0] = (char)(c | UTF8_cval1);
1301     return 1;
1302   }
1303   if (c < min3) {
1304     buf[0] = (char)((c >> 6) | UTF8_cval2);
1305     buf[1] = (char)((c & 0x3f) | 0x80);
1306     return 2;
1307   }
1308   if (c < min4) {
1309     buf[0] = (char)((c >> 12) | UTF8_cval3);
1310     buf[1] = (char)(((c >> 6) & 0x3f) | 0x80);
1311     buf[2] = (char)((c & 0x3f) | 0x80);
1312     return 3;
1313   }
1314   if (c < 0x110000) {
1315     buf[0] = (char)((c >> 18) | UTF8_cval4);
1316     buf[1] = (char)(((c >> 12) & 0x3f) | 0x80);
1317     buf[2] = (char)(((c >> 6) & 0x3f) | 0x80);
1318     buf[3] = (char)((c & 0x3f) | 0x80);
1319     return 4;
1320   }
1321   return 0;
1322 }
1323 
1324 int FASTCALL
1325 XmlUtf16Encode(int charNum, unsigned short *buf)
1326 {
1327   if (charNum < 0)
1328     return 0;
1329   if (charNum < 0x10000) {
1330     buf[0] = (unsigned short)charNum;
1331     return 1;
1332   }
1333   if (charNum < 0x110000) {
1334     charNum -= 0x10000;
1335     buf[0] = (unsigned short)((charNum >> 10) + 0xD800);
1336     buf[1] = (unsigned short)((charNum & 0x3FF) + 0xDC00);
1337     return 2;
1338   }
1339   return 0;
1340 }
1341 
1342 struct unknown_encoding {
1343   struct normal_encoding normal;
1344   CONVERTER convert;
1345   void *userData;
1346   unsigned short utf16[256];
1347   char utf8[256][4];
1348 };
1349 
1350 #define AS_UNKNOWN_ENCODING(enc)  ((const struct unknown_encoding *) (enc))
1351 
1352 int
1353 XmlSizeOfUnknownEncoding(void)
1354 {
1355   return sizeof(struct unknown_encoding);
1356 }
1357 
1358 static int PTRFASTCALL
1359 unknown_isName(const ENCODING *enc, const char *p)
1360 {
1361   const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1362   int c = uenc->convert(uenc->userData, p);
1363   if (c & ~0xFFFF)
1364     return 0;
1365   return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
1366 }
1367 
1368 static int PTRFASTCALL
1369 unknown_isNmstrt(const ENCODING *enc, const char *p)
1370 {
1371   const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1372   int c = uenc->convert(uenc->userData, p);
1373   if (c & ~0xFFFF)
1374     return 0;
1375   return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
1376 }
1377 
1378 static int PTRFASTCALL
1379 unknown_isInvalid(const ENCODING *enc, const char *p)
1380 {
1381   const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1382   int c = uenc->convert(uenc->userData, p);
1383   return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1384 }
1385 
1386 static enum XML_Convert_Result PTRCALL
1387 unknown_toUtf8(const ENCODING *enc,
1388                const char **fromP, const char *fromLim,
1389                char **toP, const char *toLim)
1390 {
1391   const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1392   char buf[XML_UTF8_ENCODE_MAX];
1393   for (;;) {
1394     const char *utf8;
1395     int n;
1396     if (*fromP == fromLim)
1397       return XML_CONVERT_COMPLETED;
1398     utf8 = uenc->utf8[(unsigned char)**fromP];
1399     n = *utf8++;
1400     if (n == 0) {
1401       int c = uenc->convert(uenc->userData, *fromP);
1402       n = XmlUtf8Encode(c, buf);
1403       if (n > toLim - *toP)
1404         return XML_CONVERT_OUTPUT_EXHAUSTED;
1405       utf8 = buf;
1406       *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1407                  - (BT_LEAD2 - 2));
1408     }
1409     else {
1410       if (n > toLim - *toP)
1411         return XML_CONVERT_OUTPUT_EXHAUSTED;
1412       (*fromP)++;
1413     }
1414     do {
1415       *(*toP)++ = *utf8++;
1416     } while (--n != 0);
1417   }
1418 }
1419 
1420 static enum XML_Convert_Result PTRCALL
1421 unknown_toUtf16(const ENCODING *enc,
1422                 const char **fromP, const char *fromLim,
1423                 unsigned short **toP, const unsigned short *toLim)
1424 {
1425   const struct unknown_encoding *uenc = AS_UNKNOWN_ENCODING(enc);
1426   while (*fromP < fromLim && *toP < toLim) {
1427     unsigned short c = uenc->utf16[(unsigned char)**fromP];
1428     if (c == 0) {
1429       c = (unsigned short)
1430           uenc->convert(uenc->userData, *fromP);
1431       *fromP += (AS_NORMAL_ENCODING(enc)->type[(unsigned char)**fromP]
1432                  - (BT_LEAD2 - 2));
1433     }
1434     else
1435       (*fromP)++;
1436     *(*toP)++ = c;
1437   }
1438 
1439   if ((*toP == toLim) && (*fromP < fromLim))
1440     return XML_CONVERT_OUTPUT_EXHAUSTED;
1441   else
1442     return XML_CONVERT_COMPLETED;
1443 }
1444 
1445 ENCODING *
1446 XmlInitUnknownEncoding(void *mem,
1447                        int *table,
1448                        CONVERTER convert,
1449                        void *userData)
1450 {
1451   int i;
1452   struct unknown_encoding *e = (struct unknown_encoding *)mem;
1453   for (i = 0; i < (int)sizeof(struct normal_encoding); i++)
1454     ((char *)mem)[i] = ((char *)&latin1_encoding)[i];
1455   for (i = 0; i < 128; i++)
1456     if (latin1_encoding.type[i] != BT_OTHER
1457         && latin1_encoding.type[i] != BT_NONXML
1458         && table[i] != i)
1459       return 0;
1460   for (i = 0; i < 256; i++) {
1461     int c = table[i];
1462     if (c == -1) {
1463       e->normal.type[i] = BT_MALFORM;
1464       /* This shouldn't really get used. */
1465       e->utf16[i] = 0xFFFF;
1466       e->utf8[i][0] = 1;
1467       e->utf8[i][1] = 0;
1468     }
1469     else if (c < 0) {
1470       if (c < -4)
1471         return 0;
1472       e->normal.type[i] = (unsigned char)(BT_LEAD2 - (c + 2));
1473       e->utf8[i][0] = 0;
1474       e->utf16[i] = 0;
1475     }
1476     else if (c < 0x80) {
1477       if (latin1_encoding.type[c] != BT_OTHER
1478           && latin1_encoding.type[c] != BT_NONXML
1479           && c != i)
1480         return 0;
1481       e->normal.type[i] = latin1_encoding.type[c];
1482       e->utf8[i][0] = 1;
1483       e->utf8[i][1] = (char)c;
1484       e->utf16[i] = (unsigned short)(c == 0 ? 0xFFFF : c);
1485     }
1486     else if (checkCharRefNumber(c) < 0) {
1487       e->normal.type[i] = BT_NONXML;
1488       /* This shouldn't really get used. */
1489       e->utf16[i] = 0xFFFF;
1490       e->utf8[i][0] = 1;
1491       e->utf8[i][1] = 0;
1492     }
1493     else {
1494       if (c > 0xFFFF)
1495         return 0;
1496       if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
1497         e->normal.type[i] = BT_NMSTRT;
1498       else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
1499         e->normal.type[i] = BT_NAME;
1500       else
1501         e->normal.type[i] = BT_OTHER;
1502       e->utf8[i][0] = (char)XmlUtf8Encode(c, e->utf8[i] + 1);
1503       e->utf16[i] = (unsigned short)c;
1504     }
1505   }
1506   e->userData = userData;
1507   e->convert = convert;
1508   if (convert) {
1509     e->normal.isName2 = unknown_isName;
1510     e->normal.isName3 = unknown_isName;
1511     e->normal.isName4 = unknown_isName;
1512     e->normal.isNmstrt2 = unknown_isNmstrt;
1513     e->normal.isNmstrt3 = unknown_isNmstrt;
1514     e->normal.isNmstrt4 = unknown_isNmstrt;
1515     e->normal.isInvalid2 = unknown_isInvalid;
1516     e->normal.isInvalid3 = unknown_isInvalid;
1517     e->normal.isInvalid4 = unknown_isInvalid;
1518   }
1519   e->normal.enc.utf8Convert = unknown_toUtf8;
1520   e->normal.enc.utf16Convert = unknown_toUtf16;
1521   return &(e->normal.enc);
1522 }
1523 
1524 /* If this enumeration is changed, getEncodingIndex and encodings
1525 must also be changed. */
1526 enum {
1527   UNKNOWN_ENC = -1,
1528   ISO_8859_1_ENC = 0,
1529   US_ASCII_ENC,
1530   UTF_8_ENC,
1531   UTF_16_ENC,
1532   UTF_16BE_ENC,
1533   UTF_16LE_ENC,
1534   /* must match encodingNames up to here */
1535   NO_ENC
1536 };
1537 
1538 static const char KW_ISO_8859_1[] = {
1539   ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8, ASCII_5, ASCII_9,
1540   ASCII_MINUS, ASCII_1, '\0'
1541 };
1542 static const char KW_US_ASCII[] = {
1543   ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S, ASCII_C, ASCII_I, ASCII_I,
1544   '\0'
1545 };
1546 static const char KW_UTF_8[] =  {
1547   ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
1548 };
1549 static const char KW_UTF_16[] = {
1550   ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
1551 };
1552 static const char KW_UTF_16BE[] = {
1553   ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_B, ASCII_E,
1554   '\0'
1555 };
1556 static const char KW_UTF_16LE[] = {
1557   ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_L, ASCII_E,
1558   '\0'
1559 };
1560 
1561 static int FASTCALL
1562 getEncodingIndex(const char *name)
1563 {
1564   static const char * const encodingNames[] = {
1565     KW_ISO_8859_1,
1566     KW_US_ASCII,
1567     KW_UTF_8,
1568     KW_UTF_16,
1569     KW_UTF_16BE,
1570     KW_UTF_16LE,
1571   };
1572   int i;
1573   if (name == NULL)
1574     return NO_ENC;
1575   for (i = 0; i < (int)(sizeof(encodingNames)/sizeof(encodingNames[0])); i++)
1576     if (streqci(name, encodingNames[i]))
1577       return i;
1578   return UNKNOWN_ENC;
1579 }
1580 
1581 /* For binary compatibility, we store the index of the encoding
1582    specified at initialization in the isUtf16 member.
1583 */
1584 
1585 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1586 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1587 
1588 /* This is what detects the encoding.  encodingTable maps from
1589    encoding indices to encodings; INIT_ENC_INDEX(enc) is the index of
1590    the external (protocol) specified encoding; state is
1591    XML_CONTENT_STATE if we're parsing an external text entity, and
1592    XML_PROLOG_STATE otherwise.
1593 */
1594 
1595 
1596 static int
1597 initScan(const ENCODING * const *encodingTable,
1598          const INIT_ENCODING *enc,
1599          int state,
1600          const char *ptr,
1601          const char *end,
1602          const char **nextTokPtr)
1603 {
1604   const ENCODING **encPtr;
1605 
1606   if (ptr >= end)
1607     return XML_TOK_NONE;
1608   encPtr = enc->encPtr;
1609   if (ptr + 1 == end) {
1610     /* only a single byte available for auto-detection */
1611 #ifndef XML_DTD /* FIXME */
1612     /* a well-formed document entity must have more than one byte */
1613     if (state != XML_CONTENT_STATE)
1614       return XML_TOK_PARTIAL;
1615 #endif
1616     /* so we're parsing an external text entity... */
1617     /* if UTF-16 was externally specified, then we need at least 2 bytes */
1618     switch (INIT_ENC_INDEX(enc)) {
1619     case UTF_16_ENC:
1620     case UTF_16LE_ENC:
1621     case UTF_16BE_ENC:
1622       return XML_TOK_PARTIAL;
1623     }
1624     switch ((unsigned char)*ptr) {
1625     case 0xFE:
1626     case 0xFF:
1627     case 0xEF: /* possibly first byte of UTF-8 BOM */
1628       if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1629           && state == XML_CONTENT_STATE)
1630         break;
1631       /* fall through */
1632     case 0x00:
1633     case 0x3C:
1634       return XML_TOK_PARTIAL;
1635     }
1636   }
1637   else {
1638     switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
1639     case 0xFEFF:
1640       if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1641           && state == XML_CONTENT_STATE)
1642         break;
1643       *nextTokPtr = ptr + 2;
1644       *encPtr = encodingTable[UTF_16BE_ENC];
1645       return XML_TOK_BOM;
1646     /* 00 3C is handled in the default case */
1647     case 0x3C00:
1648       if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
1649            || INIT_ENC_INDEX(enc) == UTF_16_ENC)
1650           && state == XML_CONTENT_STATE)
1651         break;
1652       *encPtr = encodingTable[UTF_16LE_ENC];
1653       return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1654     case 0xFFFE:
1655       if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1656           && state == XML_CONTENT_STATE)
1657         break;
1658       *nextTokPtr = ptr + 2;
1659       *encPtr = encodingTable[UTF_16LE_ENC];
1660       return XML_TOK_BOM;
1661     case 0xEFBB:
1662       /* Maybe a UTF-8 BOM (EF BB BF) */
1663       /* If there's an explicitly specified (external) encoding
1664          of ISO-8859-1 or some flavour of UTF-16
1665          and this is an external text entity,
1666          don't look for the BOM,
1667          because it might be a legal data.
1668       */
1669       if (state == XML_CONTENT_STATE) {
1670         int e = INIT_ENC_INDEX(enc);
1671         if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC
1672             || e == UTF_16LE_ENC || e == UTF_16_ENC)
1673           break;
1674       }
1675       if (ptr + 2 == end)
1676         return XML_TOK_PARTIAL;
1677       if ((unsigned char)ptr[2] == 0xBF) {
1678         *nextTokPtr = ptr + 3;
1679         *encPtr = encodingTable[UTF_8_ENC];
1680         return XML_TOK_BOM;
1681       }
1682       break;
1683     default:
1684       if (ptr[0] == '\0') {
1685         /* 0 isn't a legal data character. Furthermore a document
1686            entity can only start with ASCII characters.  So the only
1687            way this can fail to be big-endian UTF-16 if it it's an
1688            external parsed general entity that's labelled as
1689            UTF-16LE.
1690         */
1691         if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
1692           break;
1693         *encPtr = encodingTable[UTF_16BE_ENC];
1694         return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1695       }
1696       else if (ptr[1] == '\0') {
1697         /* We could recover here in the case:
1698             - parsing an external entity
1699             - second byte is 0
1700             - no externally specified encoding
1701             - no encoding declaration
1702            by assuming UTF-16LE.  But we don't, because this would mean when
1703            presented just with a single byte, we couldn't reliably determine
1704            whether we needed further bytes.
1705         */
1706         if (state == XML_CONTENT_STATE)
1707           break;
1708         *encPtr = encodingTable[UTF_16LE_ENC];
1709         return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1710       }
1711       break;
1712     }
1713   }
1714   *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1715   return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1716 }
1717 
1718 
1719 #define NS(x) x
1720 #define ns(x) x
1721 #define XML_TOK_NS_C
1722 #include "xmltok_ns.c"
1723 #undef XML_TOK_NS_C
1724 #undef NS
1725 #undef ns
1726 
1727 #ifdef XML_NS
1728 
1729 #define NS(x) x ## NS
1730 #define ns(x) x ## _ns
1731 
1732 #define XML_TOK_NS_C
1733 #include "xmltok_ns.c"
1734 #undef XML_TOK_NS_C
1735 
1736 #undef NS
1737 #undef ns
1738 
1739 ENCODING *
1740 XmlInitUnknownEncodingNS(void *mem,
1741                          int *table,
1742                          CONVERTER convert,
1743                          void *userData)
1744 {
1745   ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
1746   if (enc)
1747     ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;
1748   return enc;
1749 }
1750 
1751 #endif /* XML_NS */
1752