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