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

Unix_Linux

  1. /*
  2.  * GBK
  3.  */
  4. /*
  5.  * GBK, as described in Ken Lunde's book, is an extension of GB 2312-1980
  6.  * (shifted by adding 0x8080 to the range 0xA1A1..0xFEFE, as used in EUC-CN).
  7.  * It adds the following ranges:
  8.  *
  9.  * (part of GBK/1)  0xA2A1-0xA2AA  Small Roman numerals
  10.  * GBK/3   0x{81-A0}{40-7E,80-FE}  6080 new characters, all in Unicode
  11.  * GBK/4   0x{AA-FE}{40-7E,80-A0}  8160 new characters, 8080 in Unicode
  12.  * GBK/5   0x{A8-A9}{40-7E,80-A0}  166 new characters, 153 in Unicode
  13.  */
  14. /*
  15.  * CP936 is nearly identical to GBK. It differs as follows:
  16.  *
  17.  * 1. Some characters in the GB2312 range are defined differently:
  18.  *
  19.  *     code    GB2312                         CP936.TXT
  20.  *    0xA1A4   0x30FB # KATAKANA MIDDLE DOT   0x00B7 # MIDDLE DOT
  21.  *    0xA1AA   0x2015 # HORIZONTAL BAR        0x2014 # EM DASH
  22.  *
  23.  * 2. 19 characters added in the range 0xA6E0-0xA6F5.
  24.  *
  25.  * 3. 4 characters added in the range 0xA8BB-0xA8C0.
  26.  */
  27. /*
  28.  * Since all three tables I have looked at
  29.  *   - the CP936 table by Microsoft, found on ftp.unicode.org,
  30.  *   - the GBK table by Sun, investigated on a Solaris 2.7 machine,
  31.  *   - the GBK tables by CWEX, found in the Big5+ package,
  32.  * all include these CP936 extensions (the CWEX tables have additional
  33.  * differences), I conclude that either Ken Lunde has overlooked some of
  34.  * the differences between GB2312 and GBK, or he is right but the major
  35.  * vendors don't care about it. In either case, CP936 is the de facto
  36.  * standard under the name "GBK", and we should better support it.
  37.  *
  38.  * So in what follows, when we write "GBK" we always mean "CP936".
  39.  */
  40. #include "gbkext1.h"
  41. #include "gbkext2.h"
  42. #include "gbkext_inv.h"
  43. #include "cp936ext.h"
  44. static int
  45. gbk_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  46. {
  47.   unsigned char c = *s;
  48.   if (c >= 0x81 && c < 0xff) {
  49.     if (n < 2)
  50.       return RET_TOOFEW(0);
  51.     if (c >= 0xa1 && c <= 0xf7) {
  52.       unsigned char c2 = s[1];
  53.       if (c == 0xa1) {
  54.         if (c2 == 0xa4) {
  55.           *pwc = 0x00b7;
  56.           return 2;
  57.         }
  58.         if (c2 == 0xaa) {
  59.           *pwc = 0x2014;
  60.           return 2;
  61.         }
  62.       }
  63.       if (c2 >= 0xa1 && c2 < 0xff) {
  64.         unsigned char buf[2];
  65.         int ret;
  66.         buf[0] = c-0x80; buf[1] = c2-0x80;
  67.         ret = gb2312_mbtowc(conv,pwc,buf,2);
  68.         if (ret != RET_ILSEQ)
  69.           return ret;
  70.         buf[0] = c; buf[1] = c2;
  71.         ret = cp936ext_mbtowc(conv,pwc,buf,2);
  72.         if (ret != RET_ILSEQ)
  73.           return ret;
  74.       }
  75.     }
  76.     if (c >= 0x81 && c <= 0xa0)
  77.       return gbkext1_mbtowc(conv,pwc,s,2);
  78.     if (c >= 0xa8 && c <= 0xfe)
  79.       return gbkext2_mbtowc(conv,pwc,s,2);
  80.     if (c == 0xa2) {
  81.       unsigned char c2 = s[1];
  82.       if (c2 >= 0xa1 && c2 <= 0xaa) {
  83.         *pwc = 0x2170+(c2-0xa1);
  84.         return 2;
  85.       }
  86.     }
  87.   }
  88.   return RET_ILSEQ;
  89. }
  90. static int
  91. gbk_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  92. {
  93.   unsigned char buf[2];
  94.   int ret;
  95.   if (wc != 0x30fb && wc != 0x2015) {
  96.     ret = gb2312_wctomb(conv,buf,wc,2);
  97.     if (ret != RET_ILSEQ) {
  98.       if (ret != 2) abort();
  99.       if (n < 2)
  100.         return RET_TOOSMALL;
  101.       r[0] = buf[0]+0x80;
  102.       r[1] = buf[1]+0x80;
  103.       return 2;
  104.     }
  105.   }
  106.   ret = gbkext_inv_wctomb(conv,buf,wc,2);
  107.   if (ret != RET_ILSEQ) {
  108.     if (ret != 2) abort();
  109.     if (n < 2)
  110.       return RET_TOOSMALL;
  111.     r[0] = buf[0];
  112.     r[1] = buf[1];
  113.     return 2;
  114.   }
  115.   if (wc >= 0x2170 && wc <= 0x2179) {
  116.     r[0] = 0xa2;
  117.     r[1] = 0xa1 + (wc-0x2170);
  118.     return 2;
  119.   }
  120.   ret = cp936ext_wctomb(conv,buf,wc,2);
  121.   if (ret != RET_ILSEQ) {
  122.     if (ret != 2) abort();
  123.     if (n < 2)
  124.       return RET_TOOSMALL;
  125.     r[0] = buf[0];
  126.     r[1] = buf[1];
  127.     return 2;
  128.   }
  129.   if (wc == 0x00b7) {
  130.     if (n < 2)
  131.       return RET_TOOSMALL;
  132.     r[0] = 0xa1;
  133.     r[1] = 0xa4;
  134.     return 2;
  135.   }
  136.   if (wc == 0x2014) {
  137.     if (n < 2)
  138.       return RET_TOOSMALL;
  139.     r[0] = 0xa1;
  140.     r[1] = 0xaa;
  141.     return 2;
  142.   }
  143.   return RET_ILSEQ;
  144. }