2
Copyright (c) 1998, 1999 Thai Open Source Software Center Ltd
3
See the file COPYING for copying permission.
7
= "$Header: /home/cvs/apr-util/xml/expat/lib/xmltok.c,v 1.1 2001/02/28 14:41:26 gstein Exp $";
9
#ifdef COMPILED_FROM_DSP
10
# include "winconfig.h"
13
#endif /* ndef COMPILED_FROM_DSP */
19
#define IGNORE_SECTION_TOK_VTABLE , PREFIX(ignoreSectionTok)
21
#define IGNORE_SECTION_TOK_VTABLE /* as nothing */
25
{ PREFIX(prologTok), PREFIX(contentTok), \
26
PREFIX(cdataSectionTok) IGNORE_SECTION_TOK_VTABLE }, \
27
{ PREFIX(attributeValueTok), PREFIX(entityValueTok) }, \
29
PREFIX(nameMatchesAscii), \
33
PREFIX(charRefNumber), \
34
PREFIX(predefinedEntityName), \
35
PREFIX(updatePosition), \
38
#define VTABLE VTABLE1, PREFIX(toUtf8), PREFIX(toUtf16)
40
#define UCS2_GET_NAMING(pages, hi, lo) \
41
(namingBitmap[(pages[hi] << 3) + ((lo) >> 5)] & (1 << ((lo) & 0x1F)))
43
/* A 2 byte UTF-8 representation splits the characters 11 bits
44
between the bottom 5 and 6 bits of the bytes.
45
We need 8 bits to index into pages, 3 bits to add to that index and
46
5 bits to generate the mask. */
47
#define UTF8_GET_NAMING2(pages, byte) \
48
(namingBitmap[((pages)[(((byte)[0]) >> 2) & 7] << 3) \
49
+ ((((byte)[0]) & 3) << 1) \
50
+ ((((byte)[1]) >> 5) & 1)] \
51
& (1 << (((byte)[1]) & 0x1F)))
53
/* A 3 byte UTF-8 representation splits the characters 16 bits
54
between the bottom 4, 6 and 6 bits of the bytes.
55
We need 8 bits to index into pages, 3 bits to add to that index and
56
5 bits to generate the mask. */
57
#define UTF8_GET_NAMING3(pages, byte) \
58
(namingBitmap[((pages)[((((byte)[0]) & 0xF) << 4) \
59
+ ((((byte)[1]) >> 2) & 0xF)] \
61
+ ((((byte)[1]) & 3) << 1) \
62
+ ((((byte)[2]) >> 5) & 1)] \
63
& (1 << (((byte)[2]) & 0x1F)))
65
#define UTF8_GET_NAMING(pages, p, n) \
67
? UTF8_GET_NAMING2(pages, (const unsigned char *)(p)) \
69
? UTF8_GET_NAMING3(pages, (const unsigned char *)(p)) \
72
#define UTF8_INVALID3(p) \
74
? (((p)[1] & 0x20) != 0) \
76
? ((p)[1] == 0xBF && ((p)[2] == 0xBF || (p)[2] == 0xBE)) \
79
#define UTF8_INVALID4(p) ((*p) == 0xF4 && ((p)[1] & 0x30) != 0)
82
int isNever(const ENCODING *enc, const char *p)
88
int utf8_isName2(const ENCODING *enc, const char *p)
90
return UTF8_GET_NAMING2(namePages, (const unsigned char *)p);
94
int utf8_isName3(const ENCODING *enc, const char *p)
96
return UTF8_GET_NAMING3(namePages, (const unsigned char *)p);
99
#define utf8_isName4 isNever
102
int utf8_isNmstrt2(const ENCODING *enc, const char *p)
104
return UTF8_GET_NAMING2(nmstrtPages, (const unsigned char *)p);
108
int utf8_isNmstrt3(const ENCODING *enc, const char *p)
110
return UTF8_GET_NAMING3(nmstrtPages, (const unsigned char *)p);
113
#define utf8_isNmstrt4 isNever
115
#define utf8_isInvalid2 isNever
118
int utf8_isInvalid3(const ENCODING *enc, const char *p)
120
return UTF8_INVALID3((const unsigned char *)p);
124
int utf8_isInvalid4(const ENCODING *enc, const char *p)
126
return UTF8_INVALID4((const unsigned char *)p);
129
struct normal_encoding {
131
unsigned char type[256];
133
int (*byteType)(const ENCODING *, const char *);
134
int (*isNameMin)(const ENCODING *, const char *);
135
int (*isNmstrtMin)(const ENCODING *, const char *);
136
int (*byteToAscii)(const ENCODING *, const char *);
137
int (*charMatches)(const ENCODING *, const char *, int);
138
#endif /* XML_MIN_SIZE */
139
int (*isName2)(const ENCODING *, const char *);
140
int (*isName3)(const ENCODING *, const char *);
141
int (*isName4)(const ENCODING *, const char *);
142
int (*isNmstrt2)(const ENCODING *, const char *);
143
int (*isNmstrt3)(const ENCODING *, const char *);
144
int (*isNmstrt4)(const ENCODING *, const char *);
145
int (*isInvalid2)(const ENCODING *, const char *);
146
int (*isInvalid3)(const ENCODING *, const char *);
147
int (*isInvalid4)(const ENCODING *, const char *);
152
#define STANDARD_VTABLE(E) \
161
#define STANDARD_VTABLE(E) /* as nothing */
165
#define NORMAL_VTABLE(E) \
176
static int checkCharRefNumber(int);
178
#include "xmltok_impl.h"
182
#define sb_isNameMin isNever
183
#define sb_isNmstrtMin isNever
187
#define MINBPC(enc) ((enc)->minBytesPerChar)
189
/* minimum bytes per character */
190
#define MINBPC(enc) 1
193
#define SB_BYTE_TYPE(enc, p) \
194
(((struct normal_encoding *)(enc))->type[(unsigned char)*(p)])
198
int sb_byteType(const ENCODING *enc, const char *p)
200
return SB_BYTE_TYPE(enc, p);
202
#define BYTE_TYPE(enc, p) \
203
(((const struct normal_encoding *)(enc))->byteType(enc, p))
205
#define BYTE_TYPE(enc, p) SB_BYTE_TYPE(enc, p)
209
#define BYTE_TO_ASCII(enc, p) \
210
(((const struct normal_encoding *)(enc))->byteToAscii(enc, p))
212
int sb_byteToAscii(const ENCODING *enc, const char *p)
217
#define BYTE_TO_ASCII(enc, p) (*(p))
220
#define IS_NAME_CHAR(enc, p, n) \
221
(((const struct normal_encoding *)(enc))->isName ## n(enc, p))
222
#define IS_NMSTRT_CHAR(enc, p, n) \
223
(((const struct normal_encoding *)(enc))->isNmstrt ## n(enc, p))
224
#define IS_INVALID_CHAR(enc, p, n) \
225
(((const struct normal_encoding *)(enc))->isInvalid ## n(enc, p))
228
#define IS_NAME_CHAR_MINBPC(enc, p) \
229
(((const struct normal_encoding *)(enc))->isNameMin(enc, p))
230
#define IS_NMSTRT_CHAR_MINBPC(enc, p) \
231
(((const struct normal_encoding *)(enc))->isNmstrtMin(enc, p))
233
#define IS_NAME_CHAR_MINBPC(enc, p) (0)
234
#define IS_NMSTRT_CHAR_MINBPC(enc, p) (0)
238
#define CHAR_MATCHES(enc, p, c) \
239
(((const struct normal_encoding *)(enc))->charMatches(enc, p, c))
241
int sb_charMatches(const ENCODING *enc, const char *p, int c)
246
/* c is an ASCII character */
247
#define CHAR_MATCHES(enc, p, c) (*(p) == c)
250
#define PREFIX(ident) normal_ ## ident
251
#include "xmltok_impl.c"
258
#undef IS_NAME_CHAR_MINBPC
259
#undef IS_NMSTRT_CHAR
260
#undef IS_NMSTRT_CHAR_MINBPC
261
#undef IS_INVALID_CHAR
263
enum { /* UTF8_cvalN is value of masked first byte of N byte sequence */
271
void utf8_toUtf8(const ENCODING *enc,
272
const char **fromP, const char *fromLim,
273
char **toP, const char *toLim)
277
if (fromLim - *fromP > toLim - *toP) {
278
/* Avoid copying partial characters. */
279
for (fromLim = *fromP + (toLim - *toP); fromLim > *fromP; fromLim--)
280
if (((unsigned char)fromLim[-1] & 0xc0) != 0x80)
283
for (to = *toP, from = *fromP; from != fromLim; from++, to++)
290
void utf8_toUtf16(const ENCODING *enc,
291
const char **fromP, const char *fromLim,
292
unsigned short **toP, const unsigned short *toLim)
294
unsigned short *to = *toP;
295
const char *from = *fromP;
296
while (from != fromLim && to != toLim) {
297
switch (((struct normal_encoding *)enc)->type[(unsigned char)*from]) {
299
*to++ = ((from[0] & 0x1f) << 6) | (from[1] & 0x3f);
303
*to++ = ((from[0] & 0xf) << 12) | ((from[1] & 0x3f) << 6) | (from[2] & 0x3f);
311
n = ((from[0] & 0x7) << 18) | ((from[1] & 0x3f) << 12) | ((from[2] & 0x3f) << 6) | (from[3] & 0x3f);
313
to[0] = (unsigned short)((n >> 10) | 0xD800);
314
to[1] = (unsigned short)((n & 0x3FF) | 0xDC00);
329
static const struct normal_encoding utf8_encoding_ns = {
330
{ VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
332
#include "asciitab.h"
335
STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
339
static const struct normal_encoding utf8_encoding = {
340
{ VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
342
#define BT_COLON BT_NMSTRT
343
#include "asciitab.h"
347
STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
352
static const struct normal_encoding internal_utf8_encoding_ns = {
353
{ VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
355
#include "iasciitab.h"
358
STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
363
static const struct normal_encoding internal_utf8_encoding = {
364
{ VTABLE1, utf8_toUtf8, utf8_toUtf16, 1, 1, 0 },
366
#define BT_COLON BT_NMSTRT
367
#include "iasciitab.h"
371
STANDARD_VTABLE(sb_) NORMAL_VTABLE(utf8_)
375
void latin1_toUtf8(const ENCODING *enc,
376
const char **fromP, const char *fromLim,
377
char **toP, const char *toLim)
381
if (*fromP == fromLim)
383
c = (unsigned char)**fromP;
385
if (toLim - *toP < 2)
387
*(*toP)++ = ((c >> 6) | UTF8_cval2);
388
*(*toP)++ = ((c & 0x3f) | 0x80);
394
*(*toP)++ = *(*fromP)++;
400
void latin1_toUtf16(const ENCODING *enc,
401
const char **fromP, const char *fromLim,
402
unsigned short **toP, const unsigned short *toLim)
404
while (*fromP != fromLim && *toP != toLim)
405
*(*toP)++ = (unsigned char)*(*fromP)++;
410
static const struct normal_encoding latin1_encoding_ns = {
411
{ VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
413
#include "asciitab.h"
414
#include "latin1tab.h"
421
static const struct normal_encoding latin1_encoding = {
422
{ VTABLE1, latin1_toUtf8, latin1_toUtf16, 1, 0, 0 },
424
#define BT_COLON BT_NMSTRT
425
#include "asciitab.h"
427
#include "latin1tab.h"
433
void ascii_toUtf8(const ENCODING *enc,
434
const char **fromP, const char *fromLim,
435
char **toP, const char *toLim)
437
while (*fromP != fromLim && *toP != toLim)
438
*(*toP)++ = *(*fromP)++;
443
static const struct normal_encoding ascii_encoding_ns = {
444
{ VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
446
#include "asciitab.h"
454
static const struct normal_encoding ascii_encoding = {
455
{ VTABLE1, ascii_toUtf8, latin1_toUtf16, 1, 1, 0 },
457
#define BT_COLON BT_NMSTRT
458
#include "asciitab.h"
465
static int unicode_byte_type(char hi, char lo)
467
switch ((unsigned char)hi) {
468
case 0xD8: case 0xD9: case 0xDA: case 0xDB:
470
case 0xDC: case 0xDD: case 0xDE: case 0xDF:
473
switch ((unsigned char)lo) {
483
#define DEFINE_UTF16_TO_UTF8(E) \
485
void E ## toUtf8(const ENCODING *enc, \
486
const char **fromP, const char *fromLim, \
487
char **toP, const char *toLim) \
490
for (from = *fromP; from != fromLim; from += 2) { \
493
unsigned char lo = GET_LO(from); \
494
unsigned char hi = GET_HI(from); \
498
if (*toP == toLim) { \
506
case 0x1: case 0x2: case 0x3: \
507
case 0x4: case 0x5: case 0x6: case 0x7: \
508
if (toLim - *toP < 2) { \
512
*(*toP)++ = ((lo >> 6) | (hi << 2) | UTF8_cval2); \
513
*(*toP)++ = ((lo & 0x3f) | 0x80); \
516
if (toLim - *toP < 3) { \
520
/* 16 bits divided 4, 6, 6 amongst 3 bytes */ \
521
*(*toP)++ = ((hi >> 4) | UTF8_cval3); \
522
*(*toP)++ = (((hi & 0xf) << 2) | (lo >> 6) | 0x80); \
523
*(*toP)++ = ((lo & 0x3f) | 0x80); \
525
case 0xD8: case 0xD9: case 0xDA: case 0xDB: \
526
if (toLim - *toP < 4) { \
530
plane = (((hi & 0x3) << 2) | ((lo >> 6) & 0x3)) + 1; \
531
*(*toP)++ = ((plane >> 2) | UTF8_cval4); \
532
*(*toP)++ = (((lo >> 2) & 0xF) | ((plane & 0x3) << 4) | 0x80); \
534
lo2 = GET_LO(from); \
535
*(*toP)++ = (((lo & 0x3) << 4) \
536
| ((GET_HI(from) & 0x3) << 2) \
539
*(*toP)++ = ((lo2 & 0x3f) | 0x80); \
546
#define DEFINE_UTF16_TO_UTF16(E) \
548
void E ## toUtf16(const ENCODING *enc, \
549
const char **fromP, const char *fromLim, \
550
unsigned short **toP, const unsigned short *toLim) \
552
/* Avoid copying first half only of surrogate */ \
553
if (fromLim - *fromP > ((toLim - *toP) << 1) \
554
&& (GET_HI(fromLim - 2) & 0xF8) == 0xD8) \
556
for (; *fromP != fromLim && *toP != toLim; *fromP += 2) \
557
*(*toP)++ = (GET_HI(*fromP) << 8) | GET_LO(*fromP); \
560
#define SET2(ptr, ch) \
561
(((ptr)[0] = ((ch) & 0xff)), ((ptr)[1] = ((ch) >> 8)))
562
#define GET_LO(ptr) ((unsigned char)(ptr)[0])
563
#define GET_HI(ptr) ((unsigned char)(ptr)[1])
565
DEFINE_UTF16_TO_UTF8(little2_)
566
DEFINE_UTF16_TO_UTF16(little2_)
572
#define SET2(ptr, ch) \
573
(((ptr)[0] = ((ch) >> 8)), ((ptr)[1] = ((ch) & 0xFF)))
574
#define GET_LO(ptr) ((unsigned char)(ptr)[1])
575
#define GET_HI(ptr) ((unsigned char)(ptr)[0])
577
DEFINE_UTF16_TO_UTF8(big2_)
578
DEFINE_UTF16_TO_UTF16(big2_)
584
#define LITTLE2_BYTE_TYPE(enc, p) \
586
? ((struct normal_encoding *)(enc))->type[(unsigned char)*(p)] \
587
: unicode_byte_type((p)[1], (p)[0]))
588
#define LITTLE2_BYTE_TO_ASCII(enc, p) ((p)[1] == 0 ? (p)[0] : -1)
589
#define LITTLE2_CHAR_MATCHES(enc, p, c) ((p)[1] == 0 && (p)[0] == c)
590
#define LITTLE2_IS_NAME_CHAR_MINBPC(enc, p) \
591
UCS2_GET_NAMING(namePages, (unsigned char)p[1], (unsigned char)p[0])
592
#define LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
593
UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[1], (unsigned char)p[0])
598
int little2_byteType(const ENCODING *enc, const char *p)
600
return LITTLE2_BYTE_TYPE(enc, p);
604
int little2_byteToAscii(const ENCODING *enc, const char *p)
606
return LITTLE2_BYTE_TO_ASCII(enc, p);
610
int little2_charMatches(const ENCODING *enc, const char *p, int c)
612
return LITTLE2_CHAR_MATCHES(enc, p, c);
616
int little2_isNameMin(const ENCODING *enc, const char *p)
618
return LITTLE2_IS_NAME_CHAR_MINBPC(enc, p);
622
int little2_isNmstrtMin(const ENCODING *enc, const char *p)
624
return LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p);
628
#define VTABLE VTABLE1, little2_toUtf8, little2_toUtf16
630
#else /* not XML_MIN_SIZE */
633
#define PREFIX(ident) little2_ ## ident
634
#define MINBPC(enc) 2
635
/* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
636
#define BYTE_TYPE(enc, p) LITTLE2_BYTE_TYPE(enc, p)
637
#define BYTE_TO_ASCII(enc, p) LITTLE2_BYTE_TO_ASCII(enc, p)
638
#define CHAR_MATCHES(enc, p, c) LITTLE2_CHAR_MATCHES(enc, p, c)
639
#define IS_NAME_CHAR(enc, p, n) 0
640
#define IS_NAME_CHAR_MINBPC(enc, p) LITTLE2_IS_NAME_CHAR_MINBPC(enc, p)
641
#define IS_NMSTRT_CHAR(enc, p, n) (0)
642
#define IS_NMSTRT_CHAR_MINBPC(enc, p) LITTLE2_IS_NMSTRT_CHAR_MINBPC(enc, p)
644
#include "xmltok_impl.c"
651
#undef IS_NAME_CHAR_MINBPC
652
#undef IS_NMSTRT_CHAR
653
#undef IS_NMSTRT_CHAR_MINBPC
654
#undef IS_INVALID_CHAR
656
#endif /* not XML_MIN_SIZE */
660
static const struct normal_encoding little2_encoding_ns = {
662
#if XML_BYTE_ORDER == 12
669
#include "asciitab.h"
670
#include "latin1tab.h"
672
STANDARD_VTABLE(little2_)
677
static const struct normal_encoding little2_encoding = {
679
#if XML_BYTE_ORDER == 12
686
#define BT_COLON BT_NMSTRT
687
#include "asciitab.h"
689
#include "latin1tab.h"
691
STANDARD_VTABLE(little2_)
694
#if XML_BYTE_ORDER != 21
698
static const struct normal_encoding internal_little2_encoding_ns = {
701
#include "iasciitab.h"
702
#include "latin1tab.h"
704
STANDARD_VTABLE(little2_)
709
static const struct normal_encoding internal_little2_encoding = {
712
#define BT_COLON BT_NMSTRT
713
#include "iasciitab.h"
715
#include "latin1tab.h"
717
STANDARD_VTABLE(little2_)
723
#define BIG2_BYTE_TYPE(enc, p) \
725
? ((struct normal_encoding *)(enc))->type[(unsigned char)(p)[1]] \
726
: unicode_byte_type((p)[0], (p)[1]))
727
#define BIG2_BYTE_TO_ASCII(enc, p) ((p)[0] == 0 ? (p)[1] : -1)
728
#define BIG2_CHAR_MATCHES(enc, p, c) ((p)[0] == 0 && (p)[1] == c)
729
#define BIG2_IS_NAME_CHAR_MINBPC(enc, p) \
730
UCS2_GET_NAMING(namePages, (unsigned char)p[0], (unsigned char)p[1])
731
#define BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p) \
732
UCS2_GET_NAMING(nmstrtPages, (unsigned char)p[0], (unsigned char)p[1])
737
int big2_byteType(const ENCODING *enc, const char *p)
739
return BIG2_BYTE_TYPE(enc, p);
743
int big2_byteToAscii(const ENCODING *enc, const char *p)
745
return BIG2_BYTE_TO_ASCII(enc, p);
749
int big2_charMatches(const ENCODING *enc, const char *p, int c)
751
return BIG2_CHAR_MATCHES(enc, p, c);
755
int big2_isNameMin(const ENCODING *enc, const char *p)
757
return BIG2_IS_NAME_CHAR_MINBPC(enc, p);
761
int big2_isNmstrtMin(const ENCODING *enc, const char *p)
763
return BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p);
767
#define VTABLE VTABLE1, big2_toUtf8, big2_toUtf16
769
#else /* not XML_MIN_SIZE */
772
#define PREFIX(ident) big2_ ## ident
773
#define MINBPC(enc) 2
774
/* CHAR_MATCHES is guaranteed to have MINBPC bytes available. */
775
#define BYTE_TYPE(enc, p) BIG2_BYTE_TYPE(enc, p)
776
#define BYTE_TO_ASCII(enc, p) BIG2_BYTE_TO_ASCII(enc, p)
777
#define CHAR_MATCHES(enc, p, c) BIG2_CHAR_MATCHES(enc, p, c)
778
#define IS_NAME_CHAR(enc, p, n) 0
779
#define IS_NAME_CHAR_MINBPC(enc, p) BIG2_IS_NAME_CHAR_MINBPC(enc, p)
780
#define IS_NMSTRT_CHAR(enc, p, n) (0)
781
#define IS_NMSTRT_CHAR_MINBPC(enc, p) BIG2_IS_NMSTRT_CHAR_MINBPC(enc, p)
783
#include "xmltok_impl.c"
790
#undef IS_NAME_CHAR_MINBPC
791
#undef IS_NMSTRT_CHAR
792
#undef IS_NMSTRT_CHAR_MINBPC
793
#undef IS_INVALID_CHAR
795
#endif /* not XML_MIN_SIZE */
799
static const struct normal_encoding big2_encoding_ns = {
801
#if XML_BYTE_ORDER == 21
808
#include "asciitab.h"
809
#include "latin1tab.h"
811
STANDARD_VTABLE(big2_)
816
static const struct normal_encoding big2_encoding = {
818
#if XML_BYTE_ORDER == 21
825
#define BT_COLON BT_NMSTRT
826
#include "asciitab.h"
828
#include "latin1tab.h"
830
STANDARD_VTABLE(big2_)
833
#if XML_BYTE_ORDER != 12
837
static const struct normal_encoding internal_big2_encoding_ns = {
840
#include "iasciitab.h"
841
#include "latin1tab.h"
843
STANDARD_VTABLE(big2_)
848
static const struct normal_encoding internal_big2_encoding = {
851
#define BT_COLON BT_NMSTRT
852
#include "iasciitab.h"
854
#include "latin1tab.h"
856
STANDARD_VTABLE(big2_)
864
int streqci(const char *s1, const char *s2)
869
if (ASCII_a <= c1 && c1 <= ASCII_z)
870
c1 += ASCII_A - ASCII_a;
871
if (ASCII_a <= c2 && c2 <= ASCII_z)
872
c2 += ASCII_A - ASCII_a;
882
void initUpdatePosition(const ENCODING *enc, const char *ptr,
883
const char *end, POSITION *pos)
885
normal_updatePosition(&utf8_encoding.enc, ptr, end, pos);
889
int toAscii(const ENCODING *enc, const char *ptr, const char *end)
893
XmlUtf8Convert(enc, &ptr, end, &p, p + 1);
913
/* Return 1 if there's just optional white space
914
or there's an S followed by name=val. */
916
int parsePseudoAttribute(const ENCODING *enc,
919
const char **namePtr,
920
const char **nameEndPtr,
922
const char **nextTokPtr)
930
if (!isSpace(toAscii(enc, ptr, end))) {
935
ptr += enc->minBytesPerChar;
936
} while (isSpace(toAscii(enc, ptr, end)));
943
c = toAscii(enc, ptr, end);
948
if (c == ASCII_EQUALS) {
955
ptr += enc->minBytesPerChar;
956
} while (isSpace(c = toAscii(enc, ptr, end)));
957
if (c != ASCII_EQUALS) {
963
ptr += enc->minBytesPerChar;
965
if (ptr == *namePtr) {
969
ptr += enc->minBytesPerChar;
970
c = toAscii(enc, ptr, end);
972
ptr += enc->minBytesPerChar;
973
c = toAscii(enc, ptr, end);
975
if (c != ASCII_QUOT && c != ASCII_APOS) {
980
ptr += enc->minBytesPerChar;
982
for (;; ptr += enc->minBytesPerChar) {
983
c = toAscii(enc, ptr, end);
986
if (!(ASCII_a <= c && c <= ASCII_z)
987
&& !(ASCII_A <= c && c <= ASCII_Z)
988
&& !(ASCII_0 <= c && c <= ASCII_9)
991
&& c != ASCII_UNDERSCORE) {
996
*nextTokPtr = ptr + enc->minBytesPerChar;
1000
static const char KW_version[] = {
1001
ASCII_v, ASCII_e, ASCII_r, ASCII_s, ASCII_i, ASCII_o, ASCII_n, '\0'
1004
static const char KW_encoding[] = {
1005
ASCII_e, ASCII_n, ASCII_c, ASCII_o, ASCII_d, ASCII_i, ASCII_n, ASCII_g, '\0'
1008
static const char KW_standalone[] = {
1009
ASCII_s, ASCII_t, ASCII_a, ASCII_n, ASCII_d, ASCII_a, ASCII_l, ASCII_o, ASCII_n, ASCII_e, '\0'
1012
static const char KW_yes[] = {
1013
ASCII_y, ASCII_e, ASCII_s, '\0'
1016
static const char KW_no[] = {
1017
ASCII_n, ASCII_o, '\0'
1021
int doParseXmlDecl(const ENCODING *(*encodingFinder)(const ENCODING *,
1024
int isGeneralTextEntity,
1025
const ENCODING *enc,
1028
const char **badPtr,
1029
const char **versionPtr,
1030
const char **versionEndPtr,
1031
const char **encodingName,
1032
const ENCODING **encoding,
1035
const char *val = 0;
1036
const char *name = 0;
1037
const char *nameEnd = 0;
1038
ptr += 5 * enc->minBytesPerChar;
1039
end -= 2 * enc->minBytesPerChar;
1040
if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr) || !name) {
1044
if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_version)) {
1045
if (!isGeneralTextEntity) {
1054
*versionEndPtr = ptr;
1055
if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1060
if (isGeneralTextEntity) {
1061
/* a TextDecl must have an EncodingDecl */
1068
if (XmlNameMatchesAscii(enc, name, nameEnd, KW_encoding)) {
1069
int c = toAscii(enc, val, end);
1070
if (!(ASCII_a <= c && c <= ASCII_z) && !(ASCII_A <= c && c <= ASCII_Z)) {
1075
*encodingName = val;
1077
*encoding = encodingFinder(enc, val, ptr - enc->minBytesPerChar);
1078
if (!parsePseudoAttribute(enc, ptr, end, &name, &nameEnd, &val, &ptr)) {
1085
if (!XmlNameMatchesAscii(enc, name, nameEnd, KW_standalone) || isGeneralTextEntity) {
1089
if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_yes)) {
1093
else if (XmlNameMatchesAscii(enc, val, ptr - enc->minBytesPerChar, KW_no)) {
1101
while (isSpace(toAscii(enc, ptr, end)))
1102
ptr += enc->minBytesPerChar;
1111
int checkCharRefNumber(int result)
1113
switch (result >> 8) {
1114
case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1115
case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1118
if (latin1_encoding.type[result] == BT_NONXML)
1122
if (result == 0xFFFE || result == 0xFFFF)
1129
int XmlUtf8Encode(int c, char *buf)
1132
/* minN is minimum legal resulting value for N byte sequence */
1141
buf[0] = (c | UTF8_cval1);
1145
buf[0] = ((c >> 6) | UTF8_cval2);
1146
buf[1] = ((c & 0x3f) | 0x80);
1150
buf[0] = ((c >> 12) | UTF8_cval3);
1151
buf[1] = (((c >> 6) & 0x3f) | 0x80);
1152
buf[2] = ((c & 0x3f) | 0x80);
1156
buf[0] = ((c >> 18) | UTF8_cval4);
1157
buf[1] = (((c >> 12) & 0x3f) | 0x80);
1158
buf[2] = (((c >> 6) & 0x3f) | 0x80);
1159
buf[3] = ((c & 0x3f) | 0x80);
1165
int XmlUtf16Encode(int charNum, unsigned short *buf)
1169
if (charNum < 0x10000) {
1173
if (charNum < 0x110000) {
1175
buf[0] = (charNum >> 10) + 0xD800;
1176
buf[1] = (charNum & 0x3FF) + 0xDC00;
1182
struct unknown_encoding {
1183
struct normal_encoding normal;
1184
int (*convert)(void *userData, const char *p);
1186
unsigned short utf16[256];
1190
int XmlSizeOfUnknownEncoding(void)
1192
return sizeof(struct unknown_encoding);
1196
int unknown_isName(const ENCODING *enc, const char *p)
1198
int c = ((const struct unknown_encoding *)enc)
1199
->convert(((const struct unknown_encoding *)enc)->userData, p);
1202
return UCS2_GET_NAMING(namePages, c >> 8, c & 0xFF);
1206
int unknown_isNmstrt(const ENCODING *enc, const char *p)
1208
int c = ((const struct unknown_encoding *)enc)
1209
->convert(((const struct unknown_encoding *)enc)->userData, p);
1212
return UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xFF);
1216
int unknown_isInvalid(const ENCODING *enc, const char *p)
1218
int c = ((const struct unknown_encoding *)enc)
1219
->convert(((const struct unknown_encoding *)enc)->userData, p);
1220
return (c & ~0xFFFF) || checkCharRefNumber(c) < 0;
1224
void unknown_toUtf8(const ENCODING *enc,
1225
const char **fromP, const char *fromLim,
1226
char **toP, const char *toLim)
1228
char buf[XML_UTF8_ENCODE_MAX];
1232
if (*fromP == fromLim)
1234
utf8 = ((const struct unknown_encoding *)enc)->utf8[(unsigned char)**fromP];
1237
int c = ((const struct unknown_encoding *)enc)
1238
->convert(((const struct unknown_encoding *)enc)->userData, *fromP);
1239
n = XmlUtf8Encode(c, buf);
1240
if (n > toLim - *toP)
1243
*fromP += ((const struct normal_encoding *)enc)->type[(unsigned char)**fromP]
1247
if (n > toLim - *toP)
1252
*(*toP)++ = *utf8++;
1258
void unknown_toUtf16(const ENCODING *enc,
1259
const char **fromP, const char *fromLim,
1260
unsigned short **toP, const unsigned short *toLim)
1262
while (*fromP != fromLim && *toP != toLim) {
1264
= ((const struct unknown_encoding *)enc)->utf16[(unsigned char)**fromP];
1266
c = (unsigned short)((const struct unknown_encoding *)enc)
1267
->convert(((const struct unknown_encoding *)enc)->userData, *fromP);
1268
*fromP += ((const struct normal_encoding *)enc)->type[(unsigned char)**fromP]
1278
XmlInitUnknownEncoding(void *mem,
1280
int (*convert)(void *userData, const char *p),
1284
struct unknown_encoding *e = mem;
1285
for (i = 0; i < (int)sizeof(struct normal_encoding); i++)
1286
((char *)mem)[i] = ((char *)&latin1_encoding)[i];
1287
for (i = 0; i < 128; i++)
1288
if (latin1_encoding.type[i] != BT_OTHER
1289
&& latin1_encoding.type[i] != BT_NONXML
1292
for (i = 0; i < 256; i++) {
1295
e->normal.type[i] = BT_MALFORM;
1296
/* This shouldn't really get used. */
1297
e->utf16[i] = 0xFFFF;
1304
e->normal.type[i] = BT_LEAD2 - (c + 2);
1308
else if (c < 0x80) {
1309
if (latin1_encoding.type[c] != BT_OTHER
1310
&& latin1_encoding.type[c] != BT_NONXML
1313
e->normal.type[i] = latin1_encoding.type[c];
1315
e->utf8[i][1] = (char)c;
1316
e->utf16[i] = c == 0 ? 0xFFFF : c;
1318
else if (checkCharRefNumber(c) < 0) {
1319
e->normal.type[i] = BT_NONXML;
1320
/* This shouldn't really get used. */
1321
e->utf16[i] = 0xFFFF;
1328
if (UCS2_GET_NAMING(nmstrtPages, c >> 8, c & 0xff))
1329
e->normal.type[i] = BT_NMSTRT;
1330
else if (UCS2_GET_NAMING(namePages, c >> 8, c & 0xff))
1331
e->normal.type[i] = BT_NAME;
1333
e->normal.type[i] = BT_OTHER;
1334
e->utf8[i][0] = (char)XmlUtf8Encode(c, e->utf8[i] + 1);
1338
e->userData = userData;
1339
e->convert = convert;
1341
e->normal.isName2 = unknown_isName;
1342
e->normal.isName3 = unknown_isName;
1343
e->normal.isName4 = unknown_isName;
1344
e->normal.isNmstrt2 = unknown_isNmstrt;
1345
e->normal.isNmstrt3 = unknown_isNmstrt;
1346
e->normal.isNmstrt4 = unknown_isNmstrt;
1347
e->normal.isInvalid2 = unknown_isInvalid;
1348
e->normal.isInvalid3 = unknown_isInvalid;
1349
e->normal.isInvalid4 = unknown_isInvalid;
1351
e->normal.enc.utf8Convert = unknown_toUtf8;
1352
e->normal.enc.utf16Convert = unknown_toUtf16;
1353
return &(e->normal.enc);
1356
/* If this enumeration is changed, getEncodingIndex and encodings
1357
must also be changed. */
1366
/* must match encodingNames up to here */
1370
static const char KW_ISO_8859_1[] = {
1371
ASCII_I, ASCII_S, ASCII_O, ASCII_MINUS, ASCII_8, ASCII_8, ASCII_5, ASCII_9, ASCII_MINUS, ASCII_1, '\0'
1373
static const char KW_US_ASCII[] = {
1374
ASCII_U, ASCII_S, ASCII_MINUS, ASCII_A, ASCII_S, ASCII_C, ASCII_I, ASCII_I, '\0'
1376
static const char KW_UTF_8[] = {
1377
ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_8, '\0'
1379
static const char KW_UTF_16[] = {
1380
ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, '\0'
1382
static const char KW_UTF_16BE[] = {
1383
ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_B, ASCII_E, '\0'
1385
static const char KW_UTF_16LE[] = {
1386
ASCII_U, ASCII_T, ASCII_F, ASCII_MINUS, ASCII_1, ASCII_6, ASCII_L, ASCII_E, '\0'
1390
int getEncodingIndex(const char *name)
1392
static const char *encodingNames[] = {
1403
for (i = 0; i < (int)(sizeof(encodingNames)/sizeof(encodingNames[0])); i++)
1404
if (streqci(name, encodingNames[i]))
1409
/* For binary compatibility, we store the index of the encoding specified
1410
at initialization in the isUtf16 member. */
1412
#define INIT_ENC_INDEX(enc) ((int)(enc)->initEnc.isUtf16)
1413
#define SET_INIT_ENC_INDEX(enc, i) ((enc)->initEnc.isUtf16 = (char)i)
1415
/* This is what detects the encoding.
1416
encodingTable maps from encoding indices to encodings;
1417
INIT_ENC_INDEX(enc) is the index of the external (protocol) specified encoding;
1418
state is XML_CONTENT_STATE if we're parsing an external text entity,
1419
and XML_PROLOG_STATE otherwise.
1424
int initScan(const ENCODING **encodingTable,
1425
const INIT_ENCODING *enc,
1429
const char **nextTokPtr)
1431
const ENCODING **encPtr;
1434
return XML_TOK_NONE;
1435
encPtr = enc->encPtr;
1436
if (ptr + 1 == end) {
1437
/* only a single byte available for auto-detection */
1438
#ifndef XML_DTD /* FIXME */
1439
/* a well-formed document entity must have more than one byte */
1440
if (state != XML_CONTENT_STATE)
1441
return XML_TOK_PARTIAL;
1443
/* so we're parsing an external text entity... */
1444
/* if UTF-16 was externally specified, then we need at least 2 bytes */
1445
switch (INIT_ENC_INDEX(enc)) {
1449
return XML_TOK_PARTIAL;
1451
switch ((unsigned char)*ptr) {
1454
case 0xEF: /* possibly first byte of UTF-8 BOM */
1455
if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1456
&& state == XML_CONTENT_STATE)
1461
return XML_TOK_PARTIAL;
1465
switch (((unsigned char)ptr[0] << 8) | (unsigned char)ptr[1]) {
1467
if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1468
&& state == XML_CONTENT_STATE)
1470
*nextTokPtr = ptr + 2;
1471
*encPtr = encodingTable[UTF_16BE_ENC];
1473
/* 00 3C is handled in the default case */
1475
if ((INIT_ENC_INDEX(enc) == UTF_16BE_ENC
1476
|| INIT_ENC_INDEX(enc) == UTF_16_ENC)
1477
&& state == XML_CONTENT_STATE)
1479
*encPtr = encodingTable[UTF_16LE_ENC];
1480
return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1482
if (INIT_ENC_INDEX(enc) == ISO_8859_1_ENC
1483
&& state == XML_CONTENT_STATE)
1485
*nextTokPtr = ptr + 2;
1486
*encPtr = encodingTable[UTF_16LE_ENC];
1489
/* Maybe a UTF-8 BOM (EF BB BF) */
1490
/* If there's an explicitly specified (external) encoding
1491
of ISO-8859-1 or some flavour of UTF-16
1492
and this is an external text entity,
1493
don't look for the BOM,
1494
because it might be a legal data. */
1495
if (state == XML_CONTENT_STATE) {
1496
int e = INIT_ENC_INDEX(enc);
1497
if (e == ISO_8859_1_ENC || e == UTF_16BE_ENC || e == UTF_16LE_ENC || e == UTF_16_ENC)
1501
return XML_TOK_PARTIAL;
1502
if ((unsigned char)ptr[2] == 0xBF) {
1503
*nextTokPtr = ptr + 3;
1504
*encPtr = encodingTable[UTF_8_ENC];
1509
if (ptr[0] == '\0') {
1510
/* 0 isn't a legal data character. Furthermore a document entity can only
1511
start with ASCII characters. So the only way this can fail to be big-endian
1512
UTF-16 if it it's an external parsed general entity that's labelled as
1514
if (state == XML_CONTENT_STATE && INIT_ENC_INDEX(enc) == UTF_16LE_ENC)
1516
*encPtr = encodingTable[UTF_16BE_ENC];
1517
return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1519
else if (ptr[1] == '\0') {
1520
/* We could recover here in the case:
1521
- parsing an external entity
1523
- no externally specified encoding
1524
- no encoding declaration
1525
by assuming UTF-16LE. But we don't, because this would mean when
1526
presented just with a single byte, we couldn't reliably determine
1527
whether we needed further bytes. */
1528
if (state == XML_CONTENT_STATE)
1530
*encPtr = encodingTable[UTF_16LE_ENC];
1531
return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1536
*encPtr = encodingTable[INIT_ENC_INDEX(enc)];
1537
return XmlTok(*encPtr, state, ptr, end, nextTokPtr);
1543
#include "xmltok_ns.c"
1549
#define NS(x) x ## NS
1550
#define ns(x) x ## _ns
1552
#include "xmltok_ns.c"
1558
XmlInitUnknownEncodingNS(void *mem,
1560
int (*convert)(void *userData, const char *p),
1563
ENCODING *enc = XmlInitUnknownEncoding(mem, table, convert, userData);
1565
((struct normal_encoding *)enc)->type[ASCII_COLON] = BT_COLON;