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