cp950ext.h
Upload User: yingmei828
Upload Date: 2007-01-01
Package Size: 1646k
Code Size: 5k
Development Platform:

Unix_Linux

  1. /*
  2.  * CP950 extensions
  3.  */
  4. static const unsigned short cp950ext_2uni_pagef9[157-116] = {
  5.   /* 0xf9 */
  6.                                   0x7881, 0x92b9, 0x88cf, 0x58bb,
  7.   0x6052, 0x7ca7, 0x5afa, 0x2554, 0x2566, 0x2557, 0x2560, 0x256c,
  8.   0x2563, 0x255a, 0x2569, 0x255d, 0x2552, 0x2564, 0x2555, 0x255e,
  9.   0x256a, 0x2561, 0x2558, 0x2567, 0x255b, 0x2553, 0x2565, 0x2556,
  10.   0x255f, 0x256b, 0x2562, 0x2559, 0x2568, 0x255c, 0x2551, 0x2550,
  11.   0x256d, 0x256e, 0x2570, 0x256f, 0x2593,
  12. };
  13. static int
  14. cp950ext_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  15. {
  16.   unsigned char c1 = s[0];
  17.   if ((c1 == 0xf9)) {
  18.     if (n >= 2) {
  19.       unsigned char c2 = s[1];
  20.       if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0xa1 && c2 < 0xff)) {
  21.         unsigned int i = 157 * (c1 - 0xa1) + (c2 - (c2 >= 0xa1 ? 0x62 : 0x40));
  22.         unsigned short wc = 0xfffd;
  23.         {
  24.           if (i >= 13932 && i < 13973)
  25.             wc = cp950ext_2uni_pagef9[i-13932];
  26.         }
  27.         if (wc != 0xfffd) {
  28.           *pwc = (wchar_t) wc;
  29.           return 2;
  30.         }
  31.       }
  32.       return RET_ILSEQ;
  33.     }
  34.     return RET_TOOFEW(0);
  35.   }
  36.   return RET_ILSEQ;
  37. }
  38. static const unsigned short cp950ext_2charset[41] = {
  39.   0xf9f9, 0xf9f8, 0xf9e6, 0xf9ef, 0xf9dd, 0xf9e8, 0xf9f1, 0xf9df,
  40.   0xf9ec, 0xf9f5, 0xf9e3, 0xf9ee, 0xf9f7, 0xf9e5, 0xf9e9, 0xf9f2,
  41.   0xf9e0, 0xf9eb, 0xf9f4, 0xf9e2, 0xf9e7, 0xf9f0, 0xf9de, 0xf9ed,
  42.   0xf9f6, 0xf9e4, 0xf9ea, 0xf9f3, 0xf9e1, 0xf9fa, 0xf9fb, 0xf9fd,
  43.   0xf9fc, 0xf9fe, 0xf9d9, 0xf9dc, 0xf9da, 0xf9d6, 0xf9db, 0xf9d8,
  44.   0xf9d7,
  45. };
  46. static const Summary16 cp950ext_uni2indx_page25[10] = {
  47.   /* 0x2500 */
  48.   {    0, 0x0000 }, {    0, 0x0000 }, {    0, 0x0000 }, {    0, 0x0000 },
  49.   {    0, 0x0000 }, {    0, 0xffff }, {   16, 0xffff }, {   32, 0x0001 },
  50.   {   33, 0x0000 }, {   33, 0x0008 },
  51. };
  52. static const Summary16 cp950ext_uni2indx_page58[12] = {
  53.   /* 0x5800 */
  54.   {   34, 0x0000 }, {   34, 0x0000 }, {   34, 0x0000 }, {   34, 0x0000 },
  55.   {   34, 0x0000 }, {   34, 0x0000 }, {   34, 0x0000 }, {   34, 0x0000 },
  56.   {   34, 0x0000 }, {   34, 0x0000 }, {   34, 0x0000 }, {   34, 0x0800 },
  57. };
  58. static const Summary16 cp950ext_uni2indx_page5a[16] = {
  59.   /* 0x5a00 */
  60.   {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0000 },
  61.   {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0000 },
  62.   {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0000 },
  63.   {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0000 }, {   35, 0x0400 },
  64. };
  65. static const Summary16 cp950ext_uni2indx_page60[6] = {
  66.   /* 0x6000 */
  67.   {   36, 0x0000 }, {   36, 0x0000 }, {   36, 0x0000 }, {   36, 0x0000 },
  68.   {   36, 0x0000 }, {   36, 0x0004 },
  69. };
  70. static const Summary16 cp950ext_uni2indx_page78[9] = {
  71.   /* 0x7800 */
  72.   {   37, 0x0000 }, {   37, 0x0000 }, {   37, 0x0000 }, {   37, 0x0000 },
  73.   {   37, 0x0000 }, {   37, 0x0000 }, {   37, 0x0000 }, {   37, 0x0000 },
  74.   {   37, 0x0002 },
  75. };
  76. static const Summary16 cp950ext_uni2indx_page7c[11] = {
  77.   /* 0x7c00 */
  78.   {   38, 0x0000 }, {   38, 0x0000 }, {   38, 0x0000 }, {   38, 0x0000 },
  79.   {   38, 0x0000 }, {   38, 0x0000 }, {   38, 0x0000 }, {   38, 0x0000 },
  80.   {   38, 0x0000 }, {   38, 0x0000 }, {   38, 0x0080 },
  81. };
  82. static const Summary16 cp950ext_uni2indx_page88[13] = {
  83.   /* 0x8800 */
  84.   {   39, 0x0000 }, {   39, 0x0000 }, {   39, 0x0000 }, {   39, 0x0000 },
  85.   {   39, 0x0000 }, {   39, 0x0000 }, {   39, 0x0000 }, {   39, 0x0000 },
  86.   {   39, 0x0000 }, {   39, 0x0000 }, {   39, 0x0000 }, {   39, 0x0000 },
  87.   {   39, 0x8000 },
  88. };
  89. static const Summary16 cp950ext_uni2indx_page92[12] = {
  90.   /* 0x9200 */
  91.   {   40, 0x0000 }, {   40, 0x0000 }, {   40, 0x0000 }, {   40, 0x0000 },
  92.   {   40, 0x0000 }, {   40, 0x0000 }, {   40, 0x0000 }, {   40, 0x0000 },
  93.   {   40, 0x0000 }, {   40, 0x0000 }, {   40, 0x0000 }, {   40, 0x0200 },
  94. };
  95. static int
  96. cp950ext_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  97. {
  98.   if (n >= 2) {
  99.     const Summary16 *summary = NULL;
  100.     if (wc >= 0x2500 && wc < 0x25a0)
  101.       summary = &cp950ext_uni2indx_page25[(wc>>4)-0x250];
  102.     else if (wc >= 0x5800 && wc < 0x58c0)
  103.       summary = &cp950ext_uni2indx_page58[(wc>>4)-0x580];
  104.     else if (wc >= 0x5a00 && wc < 0x5b00)
  105.       summary = &cp950ext_uni2indx_page5a[(wc>>4)-0x5a0];
  106.     else if (wc >= 0x6000 && wc < 0x6060)
  107.       summary = &cp950ext_uni2indx_page60[(wc>>4)-0x600];
  108.     else if (wc >= 0x7800 && wc < 0x7890)
  109.       summary = &cp950ext_uni2indx_page78[(wc>>4)-0x780];
  110.     else if (wc >= 0x7c00 && wc < 0x7cb0)
  111.       summary = &cp950ext_uni2indx_page7c[(wc>>4)-0x7c0];
  112.     else if (wc >= 0x8800 && wc < 0x88d0)
  113.       summary = &cp950ext_uni2indx_page88[(wc>>4)-0x880];
  114.     else if (wc >= 0x9200 && wc < 0x92c0)
  115.       summary = &cp950ext_uni2indx_page92[(wc>>4)-0x920];
  116.     if (summary) {
  117.       unsigned short used = summary->used;
  118.       unsigned int i = wc & 0x0f;
  119.       if (used & ((unsigned short) 1 << i)) {
  120.         unsigned short c;
  121.         /* Keep in `used' only the bits 0..i-1. */
  122.         used &= ((unsigned short) 1 << i) - 1;
  123.         /* Add `summary->indx' and the number of bits set in `used'. */
  124.         used = (used & 0x5555) + ((used & 0xaaaa) >> 1);
  125.         used = (used & 0x3333) + ((used & 0xcccc) >> 2);
  126.         used = (used & 0x0f0f) + ((used & 0xf0f0) >> 4);
  127.         used = (used & 0x00ff) + (used >> 8);
  128.         c = cp950ext_2charset[summary->indx + used];
  129.         r[0] = (c >> 8); r[1] = (c & 0xff);
  130.         return 2;
  131.       }
  132.     }
  133.     return RET_ILSEQ;
  134.   }
  135.   return RET_TOOSMALL;
  136. }