home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _3db74dc1b170ed3eec315a533c8ddb24 < prev    next >
Text File  |  2000-03-15  |  39KB  |  1,543 lines

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