Updated formatting of documentation plus a little reorganization.
[cmake.git] / Utilities / cmexpat / xmltok.c
blob8ee9c7519c8571ad705b2d3632cc77142fe200fc
1 /*
2 Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
3 See the file COPYING for copying permission.
4 */
6 #include <cmexpat/expatConfig.h>
8 #include "xmltok.h"
9 #include "nametab.h"
11 #if defined(__BORLANDC__)
12 #pragma warn -8008 // Disable "condition is always true" warning.
13 #pragma warn -8066 // Disable "unreachable code" warning.
14 #endif
16 #ifdef XML_DTD
17 #define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
18 #else
19 #define IGNORE_SECTION_TOK_VTABLE /* as nothing */
20 #endif
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)
36 #define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
38 #define UCS2_GET_NAMING(pages, hi, lo) \
39 (namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
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)))
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)))
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))
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))
77 #define UTF8_INVALID4(p) ((*p) == 0xF4 && ((p)[1] & 0x30) != 0)
79 static
80 int isNever(const ENCODING *enc, const char *p)
82 cmExpatUnused(enc);
83 cmExpatUnused(p);
84 return 0;
87 static
88 int utf8_isName2(const ENCODING *enc, const char *p)
90 cmExpatUnused(enc);
91 return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
94 static
95 int utf8_isName3(const ENCODING *enc, const char *p)
97 cmExpatUnused(enc);
98 return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
101 #define utf8_isName4 isNever
103 static
104 int utf8_isNmstrt2(const ENCODING *enc, const char *p)
106 cmExpatUnused(enc);
107 return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
110 static
111 int utf8_isNmstrt3(const ENCODING *enc, const char *p)
113 cmExpatUnused(enc);
114 return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
117 #define utf8_isNmstrt4 isNever
119 #define utf8_isInvalid2 isNever
121 static
122 int utf8_isInvalid3(const ENCODING *enc, const char *p)
124 cmExpatUnused(enc);
125 return UTF8_INVALID3((const unsigned char *)p);
128 static
129 int utf8_isInvalid4(const ENCODING *enc, const char *p)
131 cmExpatUnused(enc);
132 return UTF8_INVALID4((const unsigned char *)p);
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 *);
156 #ifdef XML_MIN_SIZE
158 #define STANDARD_VTABLE(E) \
159 E ## byteType, \
160 E ## isNameMin, \
161 E ## isNmstrtMin, \
162 E ## byteToAscii, \
163 E ## charMatches,
165 #else
167 #define STANDARD_VTABLE(E) /* as nothing */
169 #endif
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
182 #define EMPTY_VTABLE(E) 0, 0, 0, 0, 0, 0, 0, 0, 0
184 static int checkCharRefNumber(int);
186 #include "xmltok_impl.h"
187 #include "ascii.h"
189 #ifdef XML_MIN_SIZE
190 #define sb_isNameMin isNever
191 #define sb_isNmstrtMin isNever
192 #endif
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
201 #define SB_BYTE_TYPE(enc, p) \
202 (((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
204 #ifdef XML_MIN_SIZE
205 static
206 int sb_byteType(const ENCODING *enc, const char *p)
208 return SB_BYTE_TYPE(enc, p);
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
216 #ifdef XML_MIN_SIZE
217 #define BYTE_TO_ASCII(enc, p) \
218 (((const struct normal_encoding *)(enc))->byteToAscii(enc, p))
219 static
220 int sb_byteToAscii(const ENCODING *enc, const char *p)
222 return *p;
224 #else
225 #define BYTE_TO_ASCII(enc, p) (*(p))
226 #endif
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))
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
245 #ifdef XML_MIN_SIZE
246 #define CHAR_MATCHES(enc, p, c) \
247 (((const struct normal_encoding *)(enc))->charMatches(enc, p, c))
248 static
249 int sb_charMatches(const ENCODING *enc, const char *p, int c)
251 return *p == c;
253 #else
254 /* c is an ASCII character */
255 #define CHAR_MATCHES(enc, p, c) (*(p) == c)
256 #endif
258 #define PREFIX(ident) normal_ ## ident
259 #include "xmltok_impl.c"
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
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
278 static
279 void utf8_toUtf8(const ENCODING *enc,
280 const char **fromP, const char *fromLim,
281 char **toP, const char *toLim)
283 char *to;
284 const char *from;
285 cmExpatUnused(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;
292 for (to = *toP, from = *fromP; from != fromLim; from++, to++)
293 *to = *from;
294 *fromP = from;
295 *toP = to;
298 static
299 void utf8_toUtf16(const ENCODING *enc,
300 const char **fromP, const char *fromLim,
301 unsigned short **toP, const unsigned short *toLim)
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:
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;
327 break;
328 default:
329 *to++ = *from++;
330 break;
333 *fromP = from;
334 *toP = to;
337 #ifdef XML_NS
338 static const struct normal_encoding utf8_encoding_ns = {
339 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
341 #include "asciitab.h"
342 #include "utf8tab.h"
344 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
346 #endif
348 static const struct normal_encoding utf8_encoding = {
349 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
351 #define BT_COLON BT_NMSTRT
352 #include "asciitab.h"
353 #undef BT_COLON
354 #include "utf8tab.h"
356 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
359 #ifdef XML_NS
361 static const struct normal_encoding internal_utf8_encoding_ns = {
362 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
364 #include "iasciitab.h"
365 #include "utf8tab.h"
367 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
370 #endif
372 static const struct normal_encoding internal_utf8_encoding = {
373 { VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
375 #define BT_COLON BT_NMSTRT
376 #include "iasciitab.h"
377 #undef BT_COLON
378 #include "utf8tab.h"
380 STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
383 static
384 void latin1_toUtf8(const ENCODING *enc,
385 const char **fromP, const char *fromLim,
386 char **toP, const char *toLim)
388 cmExpatUnused(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)++;
401 else {
402 if (*toP == toLim)
403 break;
404 *(*toP)++ = *(*fromP)++;
409 static
410 void latin1_toUtf16(const ENCODING *enc,
411 const char **fromP, const char *fromLim,
412 unsigned short **toP, const unsigned short *toLim)
414 cmExpatUnused(enc);
415 while (*fromP != fromLim && *toP != toLim)
416 *(*toP)++ = (unsigned char)*(*fromP)++;
419 #ifdef XML_NS
421 static const struct normal_encoding latin1_encoding_ns = {
422 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
424 #include "asciitab.h"
425 #include "latin1tab.h"
427 STANDARD_VTABLE(sb_) EMPTY_VTABLE(sb_)
430 #endif
432 static const struct normal_encoding latin1_encoding = {
433 { VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
435 #define BT_COLON BT_NMSTRT
436 #include "asciitab.h"
437 #undef BT_COLON
438 #include "latin1tab.h"
440 STANDARD_VTABLE(sb_) EMPTY_VTABLE(sb_)
443 static
444 void ascii_toUtf8(const ENCODING *enc,
445 const char **fromP, const char *fromLim,
446 char **toP, const char *toLim)
448 cmExpatUnused(enc);
449 while (*fromP != fromLim && *toP != toLim)
450 *(*toP)++ = *(*fromP)++;
453 #ifdef XML_NS
455 static const struct normal_encoding ascii_encoding_ns = {
456 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
458 #include "asciitab.h"
459 /* BT_NONXML == 0 */
461 STANDARD_VTABLE(sb_) EMPTY_VTABLE(sb_)
464 #endif
466 static const struct normal_encoding ascii_encoding = {
467 { VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
469 #define BT_COLON BT_NMSTRT
470 #include "asciitab.h"
471 #undef BT_COLON
472 /* BT_NONXML == 0 */
474 STANDARD_VTABLE(sb_) EMPTY_VTABLE(sb_)
477 static int unicode_byte_type(char hi, char lo)
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;
490 break;
492 return BT_NONASCII;
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) \
501 const char *from; \
502 cmExpatUnused(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; \
515 *(*toP)++ = lo; \
516 break; \
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; \
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; \
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; \
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; \
556 *fromP = from; \
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) \
565 cmExpatUnused(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); \
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])
579 DEFINE_UTF16_TO_UTF8(little2_)
580 DEFINE_UTF16_TO_UTF16(little2_)
582 #undef SET2
583 #undef GET_LO
584 #undef GET_HI
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])
591 DEFINE_UTF16_TO_UTF8(big2_)
592 DEFINE_UTF16_TO_UTF16(big2_)
594 #undef SET2
595 #undef GET_LO
596 #undef GET_HI
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])
609 #ifdef XML_MIN_SIZE
611 static
612 int little2_byteType(const ENCODING *enc, const char *p)
614 return LITTLE2_BYTE_TYPE(enc, p);
617 static
618 int little2_byteToAscii(const ENCODING *enc, const char *p)
620 return LITTLE2_BYTE_TO_ASCII(enc, p);
623 static
624 int little2_charMatches(const ENCODING *enc, const char *p, int c)
626 return LITTLE2_CHAR_MATCHES(enc, p, c);
629 static
630 int little2_isNameMin(const ENCODING *enc, const char *p)
632 return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
635 static
636 int little2_isNmstrtMin(const ENCODING *enc, const char *p)
638 return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
641 #undef VTABLE
642 #define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
644 #else /* not XML_MIN_SIZE */
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)
658 #include "xmltok_impl.c"
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
670 #endif /* not XML_MIN_SIZE */
672 #ifdef XML_NS
674 static const struct normal_encoding little2_encoding_ns = {
675 { VTABLE, 2, 0,
676 #if XML_BYTE_ORDER == 12
678 #else
680 #endif
683 #include "asciitab.h"
684 #include "latin1tab.h"
686 STANDARD_VTABLE(little2_) EMPTY_VTABLE(little2_)
689 #endif
691 static const struct normal_encoding little2_encoding = {
692 { VTABLE, 2, 0,
693 #if XML_BYTE_ORDER == 12
695 #else
697 #endif
700 #define BT_COLON BT_NMSTRT
701 #include "asciitab.h"
702 #undef BT_COLON
703 #include "latin1tab.h"
705 STANDARD_VTABLE(little2_) EMPTY_VTABLE(little2_)
708 #if XML_BYTE_ORDER != 21
710 #ifdef XML_NS
712 static const struct normal_encoding internal_little2_encoding_ns = {
713 { VTABLE, 2, 0, 1 },
715 #include "iasciitab.h"
716 #include "latin1tab.h"
718 STANDARD_VTABLE(little2_) EMPTY_VTABLE(little2_)
721 #endif
723 static const struct normal_encoding internal_little2_encoding = {
724 { VTABLE, 2, 0, 1 },
726 #define BT_COLON BT_NMSTRT
727 #include "iasciitab.h"
728 #undef BT_COLON
729 #include "latin1tab.h"
731 STANDARD_VTABLE(little2_) EMPTY_VTABLE(little2_)
734 #endif
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])
748 #ifdef XML_MIN_SIZE
750 static
751 int big2_byteType(const ENCODING *enc, const char *p)
753 return BIG2_BYTE_TYPE(enc, p);
756 static
757 int big2_byteToAscii(const ENCODING *enc, const char *p)
759 return BIG2_BYTE_TO_ASCII(enc, p);
762 static
763 int big2_charMatches(const ENCODING *enc, const char *p, int c)
765 return BIG2_CHAR_MATCHES(enc, p, c);
768 static
769 int big2_isNameMin(const ENCODING *enc, const char *p)
771 return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
774 static
775 int big2_isNmstrtMin(const ENCODING *enc, const char *p)
777 return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
780 #undef VTABLE
781 #define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
783 #else /* not XML_MIN_SIZE */
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)
797 #include "xmltok_impl.c"
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
809 #endif /* not XML_MIN_SIZE */
811 #ifdef XML_NS
813 static const struct normal_encoding big2_encoding_ns = {
814 { VTABLE, 2, 0,
815 #if XML_BYTE_ORDER == 21
817 #else
819 #endif
822 #include "asciitab.h"
823 #include "latin1tab.h"
825 STANDARD_VTABLE(big2_) EMPTY_VTABLE(big2_)
828 #endif
830 static const struct normal_encoding big2_encoding = {
831 { VTABLE, 2, 0,
832 #if XML_BYTE_ORDER == 21
834 #else
836 #endif
839 #define BT_COLON BT_NMSTRT
840 #include "asciitab.h"
841 #undef BT_COLON
842 #include "latin1tab.h"
844 STANDARD_VTABLE(big2_) EMPTY_VTABLE(big2_)
847 #if XML_BYTE_ORDER != 12
849 #ifdef XML_NS
851 static const struct normal_encoding internal_big2_encoding_ns = {
852 { VTABLE, 2, 0, 1 },
854 #include "iasciitab.h"
855 #include "latin1tab.h"
857 STANDARD_VTABLE(big2_) EMPTY_VTABLE(big2_)
860 #endif
862 static const struct normal_encoding internal_big2_encoding = {
863 { VTABLE, 2, 0, 1 },
865 #define BT_COLON BT_NMSTRT
866 #include "iasciitab.h"
867 #undef BT_COLON
868 #include "latin1tab.h"
870 STANDARD_VTABLE(big2_) EMPTY_VTABLE(big2_)
873 #endif
875 #undef PREFIX
877 static
878 int streqci(const char *s1, const char *s2)
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;
892 return 1;
895 static
896 void initUpdatePosition(const ENCODING *enc, const char *ptr,
897 const char *end, POSITION *pos)
899 cmExpatUnused(enc);
900 normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
903 static
904 int toAscii(const ENCODING *enc, const char *ptr, const char *end)
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];
915 static
916 int isSpace(int c)
918 switch (c) {
919 case 0x20:
920 case 0xD:
921 case 0xA:
922 case 0x9:
923 return 1;
925 return 0;
928 /* Return 1 if there's just optional white space
929 or there's an S followed by name=val. */
930 static
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)
939 int c;
940 char open;
941 if (ptr == end) {
942 *namePtr = 0;
943 return 1;
945 if (!isSpace(toAscii(enc, ptr, end))) {
946 *nextTokPtr = ptr;
947 return 0;
949 do {
950 ptr += enc->minBytesPerChar;
951 } while (isSpace(toAscii(enc, ptr, end)));
952 if (ptr == end) {
953 *namePtr = 0;
954 return 1;
956 *namePtr = ptr;
957 for (;;) {
958 c = toAscii(enc, ptr, end);
959 if (c == -1) {
960 *nextTokPtr = ptr;
961 return 0;
963 if (c == ASCII_EQUALS) {
964 *nameEndPtr = ptr;
965 break;
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;
976 break;
978 ptr += enc->minBytesPerChar;
980 if (ptr == *namePtr) {
981 *nextTokPtr = ptr;
982 return 0;
984 ptr += enc->minBytesPerChar;
985 c = toAscii(enc, ptr, end);
986 while (isSpace(c)) {
987 ptr += enc->minBytesPerChar;
988 c = toAscii(enc, ptr, end);
990 if (c != ASCII_QUOT && c != ASCII_APOS) {
991 *nextTokPtr = ptr;
992 return 0;
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;
1011 *nextTokPtr = ptr + enc->minBytesPerChar;
1012 return 1;
1015 static const char KW_version[] = {
1016 ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
1019 static const char KW_encoding[] = {
1020 ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
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'
1027 static const char KW_yes[] = {
1028 ASCII_y, ASCII_e, ASCII_s, '\0'
1031 static const char KW_no[] = {
1032 ASCII_n, ASCII_o, '\0'
1035 static
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)
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;
1059 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1060 if (!isGeneralTextEntity) {
1061 *badPtr = name;
1062 return 0;
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;
1074 if (!name) {
1075 if (isGeneralTextEntity) {
1076 /* a TextDecl must have an EncodingDecl */
1077 *badPtr = ptr;
1078 return 0;
1080 return 1;
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;
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;
1097 if (!name)
1098 return 1;
1100 if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone) || isGeneralTextEntity) {
1101 *badPtr = name;
1102 return 0;
1104 if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1105 if (standalone)
1106 *standalone = 1;
1108 else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1109 if (standalone)
1110 *standalone = 0;
1112 else {
1113 *badPtr = val;
1114 return 0;
1116 while (isSpace(toAscii(enc, ptr, end)))
1117 ptr += enc->minBytesPerChar;
1118 if (ptr != end) {
1119 *badPtr = ptr;
1120 return 0;
1122 return 1;
1125 static
1126 int checkCharRefNumber(int result)
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;
1141 return result;
1144 int XmlUtf8Encode(int c, char *buf)
1146 enum {
1147 /* minN is minimum legal resulting value for N byte sequence */
1148 min2 = 0x80,
1149 min3 = 0x800,
1150 min4 = 0x10000
1153 if (c < 0)
1154 return 0;
1155 if (c < min2) {
1156 buf[0] = (c | UTF8_cval1);
1157 return 1;
1159 if (c < min3) {
1160 buf[0] = ((c >> 6) | UTF8_cval2);
1161 buf[1] = ((c & 0x3f) | 0x80);
1162 return 2;
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;
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;
1177 return 0;
1180 int XmlUtf16Encode(int charNum, unsigned short *buf)
1182 if (charNum < 0)
1183 return 0;
1184 if (charNum < 0x10000) {
1185 buf[0] = charNum;
1186 return 1;
1188 if (charNum < 0x110000) {
1189 charNum -= 0x10000;
1190 buf[0] = (charNum >> 10) + 0xD800;
1191 buf[1] = (charNum & 0x3FF) + 0xDC00;
1192 return 2;
1194 return 0;
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];
1205 int XmlSizeOfUnknownEncoding(void)
1207 return sizeof(struct unknown_encoding);
1210 static
1211 int unknown_isName(const ENCODING *enc, const char *p)
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);
1220 static
1221 int unknown_isNmstrt(const ENCODING *enc, const char *p)
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);
1230 static
1231 int unknown_isInvalid(const ENCODING *enc, const char *p)
1233 int c = ((const struct unknown_encoding *)enc)
1234 ->convert(((const struct unknown_encoding *)enc)->userData, p);
1235 return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1238 static
1239 void unknown_toUtf8(const ENCODING *enc,
1240 const char **fromP, const char *fromLim,
1241 char **toP, const char *toLim)
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);
1261 else {
1262 if (n > toLim - *toP)
1263 break;
1264 (*fromP)++;
1266 do {
1267 *(*toP)++ = *utf8++;
1268 } while (--n != 0);
1272 static
1273 void unknown_toUtf16(const ENCODING *enc,
1274 const char **fromP, const char *fromLim,
1275 unsigned short **toP, const unsigned short *toLim)
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);
1286 else
1287 (*fromP)++;
1288 *(*toP)++ = c;
1292 ENCODING *
1293 XmlInitUnknownEncoding(void *mem,
1294 int *table,
1295 int (*convert)(void *userData, const char *p),
1296 void *userData)
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;
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;
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;
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;
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;
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;
1366 e->normal.enc.utf8Convert = unknown_toUtf8;
1367 e->normal.enc.utf16Convert = unknown_toUtf16;
1368 return &(e->normal.enc);
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
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'
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'
1391 static const char KW_UTF_8[] = {
1392 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
1394 static const char KW_UTF_16[] = {
1395 ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
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'
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'
1404 static
1405 int getEncodingIndex(const char *name)
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,
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;
1424 /* For binary compatibility, we store the index of the encoding specified
1425 at initialization in the isUtf16 member. */
1427 #define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1428 #define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
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.
1438 static
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)
1446 const ENCODING **encPtr;
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;
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;
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;
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;
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);
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);
1548 break;
1551 *encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1552 return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1556 #define NS(x) x
1557 #define ns(x) x
1558 #include "xmltok_ns.c"
1559 #undef NS
1560 #undef ns
1562 #ifdef XML_NS
1564 #define NS(x) x ## NS
1565 #define ns(x) x ## _ns
1567 #include "xmltok_ns.c"
1569 #undef NS
1570 #undef ns
1572 ENCODING *
1573 XmlInitUnknownEncodingNS(void *mem,
1574 int *table,
1575 int (*convert)(void *userData, const char *p),
1576 void *userData)
1578 ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
1579 if (enc)
1580 ((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;
1581 return enc;
1584 #endif /* XML_NS */