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