gbk.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 1999-2001, 2005, 2008 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
  18. * Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /*
  21. * GBK
  22. */
  23. /*
  24. * GBK, as described in Ken Lunde's book, is an extension of GB 2312-1980
  25. * (shifted by adding 0x8080 to the range 0xA1A1..0xFEFE, as used in EUC-CN).
  26. * It adds the following ranges:
  27. *
  28. * (part of GBK/1) 0xA2A1-0xA2AA Small Roman numerals
  29. * GBK/3 0x{81-A0}{40-7E,80-FE} 6080 new characters, all in Unicode
  30. * GBK/4 0x{AA-FE}{40-7E,80-A0} 8160 new characters, 8080 in Unicode
  31. * GBK/5 0x{A8-A9}{40-7E,80-A0} 166 new characters, 153 in Unicode
  32. *
  33. * Furthermore, all four tables I have looked at
  34. * - the CP936 table by Microsoft, found on ftp.unicode.org in 1999,
  35. * - the GBK table by Sun, investigated on a Solaris 2.7 machine,
  36. * - the GBK tables by CWEX, found in the Big5+ package,
  37. * - the GB18030 standard (second printing),
  38. * agree in the following extensions. (Ken Lunde must have overlooked these
  39. * differences between GB2312 and GBK. Also, the CWEX tables have additional
  40. * differences.)
  41. *
  42. * 1. Some characters in the GB2312 range are defined differently:
  43. *
  44. * code GB2312 GBK
  45. * 0xA1A4 0x30FB # KATAKANA MIDDLE DOT 0x00B7 # MIDDLE DOT
  46. * 0xA1AA 0x2015 # HORIZONTAL BAR 0x2014 # EM DASH
  47. *
  48. * 2. 19 characters added in the range 0xA6E0-0xA6F5.
  49. *
  50. * 3. 4 characters added in the range 0xA8BB-0xA8C0.
  51. *
  52. * CP936 as of 1999 was identical to GBK. However, since 1999, Microsoft has
  53. * added new mappings to CP936...
  54. */
  55. #include "gbkext1.h"
  56. #include "gbkext2.h"
  57. #include "gbkext_inv.h"
  58. #include "cp936ext.h"
  59. static int
  60. gbk_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  61. {
  62. unsigned char c = *s;
  63. if (c >= 0x81 && c < 0xff) {
  64. if (n < 2)
  65. return RET_TOOFEW(0);
  66. if (c >= 0xa1 && c <= 0xf7) {
  67. unsigned char c2 = s[1];
  68. if (c == 0xa1) {
  69. if (c2 == 0xa4) {
  70. *pwc = 0x00b7;
  71. return 2;
  72. }
  73. if (c2 == 0xaa) {
  74. *pwc = 0x2014;
  75. return 2;
  76. }
  77. }
  78. if (c2 >= 0xa1 && c2 < 0xff) {
  79. unsigned char buf[2];
  80. int ret;
  81. buf[0] = c-0x80; buf[1] = c2-0x80;
  82. ret = gb2312_mbtowc(conv,pwc,buf,2);
  83. if (ret != RET_ILSEQ)
  84. return ret;
  85. buf[0] = c; buf[1] = c2;
  86. ret = cp936ext_mbtowc(conv,pwc,buf,2);
  87. if (ret != RET_ILSEQ)
  88. return ret;
  89. }
  90. }
  91. if (c >= 0x81 && c <= 0xa0)
  92. return gbkext1_mbtowc(conv,pwc,s,2);
  93. if (c >= 0xa8 && c <= 0xfe)
  94. return gbkext2_mbtowc(conv,pwc,s,2);
  95. if (c == 0xa2) {
  96. unsigned char c2 = s[1];
  97. if (c2 >= 0xa1 && c2 <= 0xaa) {
  98. *pwc = 0x2170+(c2-0xa1);
  99. return 2;
  100. }
  101. }
  102. }
  103. return RET_ILSEQ;
  104. }
  105. static int
  106. gbk_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  107. {
  108. unsigned char buf[2];
  109. int ret;
  110. if (wc != 0x30fb && wc != 0x2015) {
  111. ret = gb2312_wctomb(conv,buf,wc,2);
  112. if (ret != RET_ILUNI) {
  113. if (ret != 2) abort();
  114. if (n < 2)
  115. return RET_TOOSMALL;
  116. r[0] = buf[0]+0x80;
  117. r[1] = buf[1]+0x80;
  118. return 2;
  119. }
  120. }
  121. ret = gbkext_inv_wctomb(conv,buf,wc,2);
  122. if (ret != RET_ILUNI) {
  123. if (ret != 2) abort();
  124. if (n < 2)
  125. return RET_TOOSMALL;
  126. r[0] = buf[0];
  127. r[1] = buf[1];
  128. return 2;
  129. }
  130. if (wc >= 0x2170 && wc <= 0x2179) {
  131. if (n < 2)
  132. return RET_TOOSMALL;
  133. r[0] = 0xa2;
  134. r[1] = 0xa1 + (wc-0x2170);
  135. return 2;
  136. }
  137. ret = cp936ext_wctomb(conv,buf,wc,2);
  138. if (ret != RET_ILUNI) {
  139. if (ret != 2) abort();
  140. if (n < 2)
  141. return RET_TOOSMALL;
  142. r[0] = buf[0];
  143. r[1] = buf[1];
  144. return 2;
  145. }
  146. if (wc == 0x00b7) {
  147. if (n < 2)
  148. return RET_TOOSMALL;
  149. r[0] = 0xa1;
  150. r[1] = 0xa4;
  151. return 2;
  152. }
  153. if (wc == 0x2014) {
  154. if (n < 2)
  155. return RET_TOOSMALL;
  156. r[0] = 0xa1;
  157. r[1] = 0xaa;
  158. return 2;
  159. }
  160. return RET_ILUNI;
  161. }