gbk.h 4.6 KB

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