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