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