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