cp932.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (C) 1999-2002, 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. * CP932
  21. */
  22. /*
  23. * Microsoft CP932 is a slightly extended version of SHIFT_JIS.
  24. * The differences between the EASTASIA/JIS/SHIFTJIS.TXT and the
  25. * VENDORS/MICSFT/WINDOWS/CP932.TXT tables found on ftp.unicode.org are
  26. * as follows:
  27. *
  28. * 1. CP932 uses ASCII, not JISX0201 Roman.
  29. *
  30. * 2. Some characters in the JISX0208 range are defined differently:
  31. *
  32. * code SHIFTJIS.TXT CP932.TXT
  33. * 0x815F 0x005C # REVERSE SOLIDUS 0xFF3C # FULLWIDTH REVERSE SOLIDUS
  34. * 0x8160 0x301C # WAVE DASH 0xFF5E # FULLWIDTH TILDE
  35. * 0x8161 0x2016 # DOUBLE VERTICAL LINE 0x2225 # PARALLEL TO
  36. * 0x817C 0x2212 # MINUS SIGN 0xFF0D # FULLWIDTH HYPHEN-MINUS
  37. * 0x8191 0x00A2 # CENT SIGN 0xFFE0 # FULLWIDTH CENT SIGN
  38. * 0x8192 0x00A3 # POUND SIGN 0xFFE1 # FULLWIDTH POUND SIGN
  39. * 0x81CA 0x00AC # NOT SIGN 0xFFE2 # FULLWIDTH NOT SIGN
  40. *
  41. * We don't implement the latter 6 of these changes, only the first one.
  42. * SHIFTJIS.TXT makes more sense. However, as a compromise with user
  43. * expectation, we implement the middle 5 of these changes in the
  44. * Unicode to CP932 direction. We don't implement the last one at all,
  45. * because it would collide with the mapping of 0xFA54.
  46. *
  47. * 3. A few new rows. See cp932ext.h.
  48. *
  49. * Many variants of CP932 (in GNU libc, JDK, OSF/1, Windows-2000, ICU) also
  50. * add:
  51. *
  52. * 4. Private area mappings:
  53. *
  54. * code Unicode
  55. * 0x{F0..F9}{40..7E,80..FC} U+E000..U+E757
  56. *
  57. * We add them too because, although there are backward compatibility problems
  58. * when a character from a private area is moved to an official Unicode code
  59. * point, they are useful for some people in practice.
  60. */
  61. #include "cp932ext.h"
  62. /*
  63. Conversion between SJIS codes (s1,s2) and JISX0208 codes (c1,c2):
  64. Example. (s1,s2) = 0x8140, (c1,c2) = 0x2121.
  65. 0x81 <= s1 <= 0x9F || 0xE0 <= s1 <= 0xEA,
  66. 0x40 <= s2 <= 0x7E || 0x80 <= s2 <= 0xFC,
  67. 0x21 <= c1 <= 0x74, 0x21 <= c2 <= 0x7E.
  68. Invariant:
  69. 94*2*(s1 < 0xE0 ? s1-0x81 : s1-0xC1) + (s2 < 0x80 ? s2-0x40 : s2-0x41)
  70. = 94*(c1-0x21)+(c2-0x21)
  71. Conversion (s1,s2) -> (c1,c2):
  72. t1 := (s1 < 0xE0 ? s1-0x81 : s1-0xC1)
  73. t2 := (s2 < 0x80 ? s2-0x40 : s2-0x41)
  74. c1 := 2*t1 + (t2 < 0x5E ? 0 : 1) + 0x21
  75. c2 := (t2 < 0x5E ? t2 : t2-0x5E) + 0x21
  76. Conversion (c1,c2) -> (s1,s2):
  77. t1 := (c1 - 0x21) >> 1
  78. t2 := ((c1 - 0x21) & 1) * 0x5E + (c2 - 0x21)
  79. s1 := (t1 < 0x1F ? t1+0x81 : t1+0xC1)
  80. s2 := (t2 < 0x3F ? t2+0x40 : t2+0x41)
  81. */
  82. static int
  83. cp932_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  84. {
  85. unsigned char c = *s;
  86. if (c < 0x80)
  87. return ascii_mbtowc(conv,pwc,s,n);
  88. else if (c >= 0xa1 && c <= 0xdf)
  89. return jisx0201_mbtowc(conv,pwc,s,n);
  90. else {
  91. unsigned char s1, s2;
  92. s1 = c;
  93. if ((s1 >= 0x81 && s1 <= 0x9f && s1 != 0x87) || (s1 >= 0xe0 && s1 <= 0xea)) {
  94. if (n < 2)
  95. return RET_TOOFEW(0);
  96. s2 = s[1];
  97. if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) {
  98. unsigned char t1 = (s1 < 0xe0 ? s1-0x81 : s1-0xc1);
  99. unsigned char t2 = (s2 < 0x80 ? s2-0x40 : s2-0x41);
  100. unsigned char buf[2];
  101. buf[0] = 2*t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
  102. buf[1] = (t2 < 0x5e ? t2 : t2-0x5e) + 0x21;
  103. return jisx0208_mbtowc(conv,pwc,buf,2);
  104. }
  105. } else if ((s1 == 0x87) || (s1 >= 0xed && s1 <= 0xee) || (s1 >= 0xfa)) {
  106. if (n < 2)
  107. return RET_TOOFEW(0);
  108. return cp932ext_mbtowc(conv,pwc,s,2);
  109. } else if (s1 >= 0xf0 && s1 <= 0xf9) {
  110. /* User-defined range. See
  111. * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  112. if (n < 2)
  113. return RET_TOOFEW(0);
  114. s2 = s[1];
  115. if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) {
  116. *pwc = 0xe000 + 188*(s1 - 0xf0) + (s2 < 0x80 ? s2-0x40 : s2-0x41);
  117. return 2;
  118. }
  119. }
  120. return RET_ILSEQ;
  121. }
  122. }
  123. static int
  124. cp932_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  125. {
  126. unsigned char buf[2];
  127. int ret;
  128. /* Try ASCII. */
  129. ret = ascii_wctomb(conv,buf,wc,1);
  130. if (ret != RET_ILUNI) {
  131. unsigned char c;
  132. if (ret != 1) abort();
  133. c = buf[0];
  134. if (c < 0x80) {
  135. r[0] = c;
  136. return 1;
  137. }
  138. }
  139. /* Try JIS X 0201-1976 Katakana. */
  140. ret = jisx0201_wctomb(conv,buf,wc,1);
  141. if (ret != RET_ILUNI) {
  142. unsigned char c;
  143. if (ret != 1) abort();
  144. c = buf[0];
  145. if (c >= 0xa1 && c <= 0xdf) {
  146. r[0] = c;
  147. return 1;
  148. }
  149. }
  150. /* Try JIS X 0208-1990. */
  151. ret = jisx0208_wctomb(conv,buf,wc,2);
  152. if (ret != RET_ILUNI) {
  153. unsigned char c1, c2;
  154. if (ret != 2) abort();
  155. if (n < 2)
  156. return RET_TOOSMALL;
  157. c1 = buf[0];
  158. c2 = buf[1];
  159. if ((c1 >= 0x21 && c1 <= 0x74) && (c2 >= 0x21 && c2 <= 0x7e)) {
  160. unsigned char t1 = (c1 - 0x21) >> 1;
  161. unsigned char t2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
  162. r[0] = (t1 < 0x1f ? t1+0x81 : t1+0xc1);
  163. r[1] = (t2 < 0x3f ? t2+0x40 : t2+0x41);
  164. return 2;
  165. }
  166. }
  167. /* Try CP932 extensions. */
  168. ret = cp932ext_wctomb(conv,buf,wc,2);
  169. if (ret != RET_ILUNI) {
  170. if (ret != 2) abort();
  171. if (n < 2)
  172. return RET_TOOSMALL;
  173. r[0] = buf[0];
  174. r[1] = buf[1];
  175. return 2;
  176. }
  177. /* User-defined range. See
  178. * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  179. if (wc >= 0xe000 && wc < 0xe758) {
  180. unsigned char c1, c2;
  181. if (n < 2)
  182. return RET_TOOSMALL;
  183. c1 = (unsigned int) (wc - 0xe000) / 188;
  184. c2 = (unsigned int) (wc - 0xe000) % 188;
  185. r[0] = c1+0xf0;
  186. r[1] = (c2 < 0x3f ? c2+0x40 : c2+0x41);
  187. return 2;
  188. }
  189. /* Irreversible mappings. */
  190. if (wc == 0xff5e) {
  191. if (n < 2)
  192. return RET_TOOSMALL;
  193. r[0] = 0x81;
  194. r[1] = 0x60;
  195. return 2;
  196. }
  197. if (wc == 0x2225) {
  198. if (n < 2)
  199. return RET_TOOSMALL;
  200. r[0] = 0x81;
  201. r[1] = 0x61;
  202. return 2;
  203. }
  204. if (wc == 0xff0d) {
  205. if (n < 2)
  206. return RET_TOOSMALL;
  207. r[0] = 0x81;
  208. r[1] = 0x7c;
  209. return 2;
  210. }
  211. if (wc == 0xffe0) {
  212. if (n < 2)
  213. return RET_TOOSMALL;
  214. r[0] = 0x81;
  215. r[1] = 0x91;
  216. return 2;
  217. }
  218. if (wc == 0xffe1) {
  219. if (n < 2)
  220. return RET_TOOSMALL;
  221. r[0] = 0x81;
  222. r[1] = 0x92;
  223. return 2;
  224. }
  225. return RET_ILUNI;
  226. }