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