cp936.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2005, 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. * CP936
  21. */
  22. /*
  23. * The IANA has CP936 as an alias of GBK. But GBK is an official Chinese
  24. * specification, whereas CP936 is de-facto maintained by Microsoft. And,
  25. * of course, Microsoft modified CP936 since 1999.
  26. *
  27. * The differences from GBK are:
  28. *
  29. * 1. A single character:
  30. *
  31. * code CP936.TXT
  32. * 0x80 0x20AC # EURO SIGN
  33. *
  34. * Some variants of CP936 (in JDK, Windows-2000, ICU) also add:
  35. *
  36. * 2. Private area mappings:
  37. *
  38. * code Unicode
  39. * 0x{A1..A2}{40..7E,80..A0} U+E4C6..U+E585
  40. * 0x{AA..AF,F8..FE}{A1..FE} U+E000..U+E4C5
  41. *
  42. * We add them too because, although there are backward compatibility problems
  43. * when a character from a private area is moved to an official Unicode code
  44. * point, they are useful for some people in practice.
  45. */
  46. static int
  47. cp936_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  48. {
  49. /* Try GBK first. */
  50. {
  51. int ret = ces_gbk_mbtowc(conv,pwc,s,n);
  52. if (ret != RET_ILSEQ)
  53. return ret;
  54. }
  55. /* Then handle the additional mappings. */
  56. {
  57. unsigned char c = *s;
  58. if (c == 0x80) {
  59. *pwc = 0x20ac;
  60. return 1;
  61. }
  62. /* User-defined characters */
  63. if (c >= 0xa1 && c <= 0xa2) {
  64. if (n < 2)
  65. return RET_TOOFEW(0);
  66. {
  67. unsigned char c2 = s[1];
  68. if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0x80 && c2 < 0xa1)) {
  69. *pwc = 0xe4c6 + 96 * (c - 0xa1) + (c2 - (c2 >= 0x80 ? 0x41 : 0x40));
  70. return 2;
  71. }
  72. }
  73. } else if ((c >= 0xaa && c < 0xb0) || (c >= 0xf8 && c < 0xff)) {
  74. if (n < 2)
  75. return RET_TOOFEW(0);
  76. {
  77. unsigned char c2 = s[1];
  78. if (c2 >= 0xa1 && c2 < 0xff) {
  79. *pwc = 0xe000 + 94 * (c - (c >= 0xf8 ? 0xf2 : 0xaa)) + (c2 - 0xa1);
  80. return 2;
  81. }
  82. }
  83. }
  84. }
  85. return RET_ILSEQ;
  86. }
  87. static int
  88. cp936_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  89. {
  90. /* Try GBK first. */
  91. {
  92. int ret = ces_gbk_wctomb(conv,r,wc,n);
  93. if (ret != RET_ILUNI)
  94. return ret;
  95. }
  96. /* Then handle the additional mappings. */
  97. if (wc >= 0xe000 && wc < 0xe586) {
  98. /* User-defined characters */
  99. if (n < 2)
  100. return RET_TOOFEW(0);
  101. if (wc < 0xe4c6) {
  102. unsigned int i = wc - 0xe000;
  103. unsigned int c1 = i / 94;
  104. unsigned int c2 = i % 94;
  105. r[0] = c1 + (c1 < 6 ? 0xaa : 0xf2);
  106. r[1] = c2 + 0xa1;
  107. return 2;
  108. } else {
  109. unsigned int i = wc - 0xe4c6;
  110. unsigned int c1 = i / 96;
  111. unsigned int c2 = i % 96;
  112. r[0] = c1 + 0xa1;
  113. r[1] = c2 + (c2 < 0x3f ? 0x40 : 0x41);
  114. return 2;
  115. }
  116. } else if (wc == 0x20ac) {
  117. r[0] = 0x80;
  118. return 1;
  119. }
  120. return RET_ILUNI;
  121. }