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