shift_jisx0213.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 1999-2002 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. * SHIFT_JISX0213
  21. */
  22. /* The structure of Shift_JISX0213 is as follows:
  23. 0x00..0x7F: ISO646-JP, an ASCII variant
  24. 0x{A1..DF}: JISX0201 Katakana.
  25. 0x{81..9F,E0..EF}{40..7E,80..FC}: JISX0213 plane 1.
  26. 0x{F0..FC}{40..7E,80..FC}: JISX0213 plane 2, with irregular row mapping.
  27. Note that some JISX0213 characters are not contained in Unicode 3.2
  28. and are therefore best represented as sequences of Unicode characters.
  29. */
  30. #include "jisx0213.h"
  31. #include "flushwc.h"
  32. static int
  33. shift_jisx0213_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  34. {
  35. ucs4_t last_wc = conv->istate;
  36. if (last_wc) {
  37. /* Output the buffered character. */
  38. conv->istate = 0;
  39. *pwc = last_wc;
  40. return 0; /* Don't advance the input pointer. */
  41. } else {
  42. unsigned char c = *s;
  43. if (c < 0x80) {
  44. /* Plain ISO646-JP character. */
  45. if (c == 0x5c)
  46. *pwc = (ucs4_t) 0x00a5;
  47. else if (c == 0x7e)
  48. *pwc = (ucs4_t) 0x203e;
  49. else
  50. *pwc = (ucs4_t) c;
  51. return 1;
  52. } else if (c >= 0xa1 && c <= 0xdf) {
  53. *pwc = c + 0xfec0;
  54. return 1;
  55. } else {
  56. if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) {
  57. /* Two byte character. */
  58. if (n >= 2) {
  59. unsigned char c2 = s[1];
  60. if ((c2 >= 0x40 && c2 <= 0x7e) || (c2 >= 0x80 && c2 <= 0xfc)) {
  61. unsigned int c1;
  62. ucs4_t wc;
  63. /* Convert to row and column. */
  64. if (c < 0xe0)
  65. c -= 0x81;
  66. else
  67. c -= 0xc1;
  68. if (c2 < 0x80)
  69. c2 -= 0x40;
  70. else
  71. c2 -= 0x41;
  72. /* Now 0 <= c <= 0x3b, 0 <= c2 <= 0xbb. */
  73. c1 = 2 * c;
  74. if (c2 >= 0x5e)
  75. c2 -= 0x5e, c1++;
  76. c2 += 0x21;
  77. if (c1 >= 0x5e) {
  78. /* Handling of JISX 0213 plane 2 rows. */
  79. if (c1 >= 0x67)
  80. c1 += 230;
  81. else if (c1 >= 0x63 || c1 == 0x5f)
  82. c1 += 168;
  83. else
  84. c1 += 162;
  85. }
  86. wc = jisx0213_to_ucs4(0x121+c1,c2);
  87. if (wc) {
  88. if (wc < 0x80) {
  89. /* It's a combining character. */
  90. ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
  91. ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
  92. /* We cannot output two Unicode characters at once. So,
  93. output the first character and buffer the second one. */
  94. *pwc = wc1;
  95. conv->istate = wc2;
  96. } else
  97. *pwc = wc;
  98. return 2;
  99. }
  100. }
  101. } else
  102. return RET_TOOFEW(0);
  103. }
  104. return RET_ILSEQ;
  105. }
  106. }
  107. }
  108. #define shift_jisx0213_flushwc normal_flushwc
  109. /* Composition tables for each of the relevant combining characters. */
  110. static const struct { unsigned short base; unsigned short composed; } shift_jisx0213_comp_table_data[] = {
  111. #define shift_jisx0213_comp_table02e5_idx 0
  112. #define shift_jisx0213_comp_table02e5_len 1
  113. { 0x8684, 0x8685 }, /* 0x12B65 = 0x12B64 U+02E5 */
  114. #define shift_jisx0213_comp_table02e9_idx (shift_jisx0213_comp_table02e5_idx+shift_jisx0213_comp_table02e5_len)
  115. #define shift_jisx0213_comp_table02e9_len 1
  116. { 0x8680, 0x8686 }, /* 0x12B66 = 0x12B60 U+02E9 */
  117. #define shift_jisx0213_comp_table0300_idx (shift_jisx0213_comp_table02e9_idx+shift_jisx0213_comp_table02e9_len)
  118. #define shift_jisx0213_comp_table0300_len 5
  119. { 0x857b, 0x8663 }, /* 0x12B44 = 0x1295C U+0300 */
  120. { 0x8657, 0x8667 }, /* 0x12B48 = 0x12B38 U+0300 */
  121. { 0x8656, 0x8669 }, /* 0x12B4A = 0x12B37 U+0300 */
  122. { 0x864f, 0x866b }, /* 0x12B4C = 0x12B30 U+0300 */
  123. { 0x8662, 0x866d }, /* 0x12B4E = 0x12B43 U+0300 */
  124. #define shift_jisx0213_comp_table0301_idx (shift_jisx0213_comp_table0300_idx+shift_jisx0213_comp_table0300_len)
  125. #define shift_jisx0213_comp_table0301_len 4
  126. { 0x8657, 0x8668 }, /* 0x12B49 = 0x12B38 U+0301 */
  127. { 0x8656, 0x866a }, /* 0x12B4B = 0x12B37 U+0301 */
  128. { 0x864f, 0x866c }, /* 0x12B4D = 0x12B30 U+0301 */
  129. { 0x8662, 0x866e }, /* 0x12B4F = 0x12B43 U+0301 */
  130. #define shift_jisx0213_comp_table309a_idx (shift_jisx0213_comp_table0301_idx+shift_jisx0213_comp_table0301_len)
  131. #define shift_jisx0213_comp_table309a_len 14
  132. { 0x82a9, 0x82f5 }, /* 0x12477 = 0x1242B U+309A */
  133. { 0x82ab, 0x82f6 }, /* 0x12478 = 0x1242D U+309A */
  134. { 0x82ad, 0x82f7 }, /* 0x12479 = 0x1242F U+309A */
  135. { 0x82af, 0x82f8 }, /* 0x1247A = 0x12431 U+309A */
  136. { 0x82b1, 0x82f9 }, /* 0x1247B = 0x12433 U+309A */
  137. { 0x834a, 0x8397 }, /* 0x12577 = 0x1252B U+309A */
  138. { 0x834c, 0x8398 }, /* 0x12578 = 0x1252D U+309A */
  139. { 0x834e, 0x8399 }, /* 0x12579 = 0x1252F U+309A */
  140. { 0x8350, 0x839a }, /* 0x1257A = 0x12531 U+309A */
  141. { 0x8352, 0x839b }, /* 0x1257B = 0x12533 U+309A */
  142. { 0x835a, 0x839c }, /* 0x1257C = 0x1253B U+309A */
  143. { 0x8363, 0x839d }, /* 0x1257D = 0x12544 U+309A */
  144. { 0x8367, 0x839e }, /* 0x1257E = 0x12548 U+309A */
  145. { 0x83f3, 0x83f6 }, /* 0x12678 = 0x12675 U+309A */
  146. };
  147. static int
  148. shift_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  149. {
  150. int count = 0;
  151. unsigned short lasttwo = conv->ostate;
  152. if (lasttwo) {
  153. /* Attempt to combine the last character with this one. */
  154. unsigned int idx;
  155. unsigned int len;
  156. if (wc == 0x02e5)
  157. idx = shift_jisx0213_comp_table02e5_idx,
  158. len = shift_jisx0213_comp_table02e5_len;
  159. else if (wc == 0x02e9)
  160. idx = shift_jisx0213_comp_table02e9_idx,
  161. len = shift_jisx0213_comp_table02e9_len;
  162. else if (wc == 0x0300)
  163. idx = shift_jisx0213_comp_table0300_idx,
  164. len = shift_jisx0213_comp_table0300_len;
  165. else if (wc == 0x0301)
  166. idx = shift_jisx0213_comp_table0301_idx,
  167. len = shift_jisx0213_comp_table0301_len;
  168. else if (wc == 0x309a)
  169. idx = shift_jisx0213_comp_table309a_idx,
  170. len = shift_jisx0213_comp_table309a_len;
  171. else
  172. goto not_combining;
  173. do
  174. if (shift_jisx0213_comp_table_data[idx].base == lasttwo)
  175. break;
  176. while (++idx, --len > 0);
  177. if (len > 0) {
  178. /* Output the combined character. */
  179. if (n >= 2) {
  180. lasttwo = shift_jisx0213_comp_table_data[idx].composed;
  181. r[0] = (lasttwo >> 8) & 0xff;
  182. r[1] = lasttwo & 0xff;
  183. conv->ostate = 0;
  184. return 2;
  185. } else
  186. return RET_TOOSMALL;
  187. }
  188. not_combining:
  189. /* Output the buffered character. */
  190. if (n < 2)
  191. return RET_TOOSMALL;
  192. r[0] = (lasttwo >> 8) & 0xff;
  193. r[1] = lasttwo & 0xff;
  194. r += 2;
  195. count = 2;
  196. }
  197. if (wc < 0x80 && wc != 0x5c && wc != 0x7e) {
  198. /* Plain ISO646-JP character. */
  199. if (n > count) {
  200. r[0] = (unsigned char) wc;
  201. conv->ostate = 0;
  202. return count+1;
  203. } else
  204. return RET_TOOSMALL;
  205. } else if (wc == 0x00a5) {
  206. if (n > count) {
  207. r[0] = 0x5c;
  208. conv->ostate = 0;
  209. return count+1;
  210. } else
  211. return RET_TOOSMALL;
  212. } else if (wc == 0x203e) {
  213. if (n > count) {
  214. r[0] = 0x7e;
  215. conv->ostate = 0;
  216. return count+1;
  217. } else
  218. return RET_TOOSMALL;
  219. } else if (wc >= 0xff61 && wc <= 0xff9f) {
  220. /* Half-width katakana. */
  221. if (n > count) {
  222. r[0] = wc - 0xfec0;
  223. conv->ostate = 0;
  224. return count+1;
  225. } else
  226. return RET_TOOSMALL;
  227. } else {
  228. unsigned int s1, s2;
  229. unsigned short jch = ucs4_to_jisx0213(wc);
  230. if (jch != 0) {
  231. /* Convert it to shifted representation. */
  232. s1 = jch >> 8;
  233. s2 = jch & 0x7f;
  234. s1 -= 0x21;
  235. s2 -= 0x21;
  236. if (s1 >= 0x5e) {
  237. /* Handling of JISX 0213 plane 2 rows. */
  238. if (s1 >= 0xcd) /* rows 0x26E..0x27E */
  239. s1 -= 102;
  240. else if (s1 >= 0x8b || s1 == 0x87) /* rows 0x228, 0x22C..0x22F */
  241. s1 -= 40;
  242. else /* rows 0x221, 0x223..0x225 */
  243. s1 -= 34;
  244. /* Now 0x5e <= s1 <= 0x77. */
  245. }
  246. if (s1 & 1)
  247. s2 += 0x5e;
  248. s1 = s1 >> 1;
  249. if (s1 < 0x1f)
  250. s1 += 0x81;
  251. else
  252. s1 += 0xc1;
  253. if (s2 < 0x3f)
  254. s2 += 0x40;
  255. else
  256. s2 += 0x41;
  257. if (jch & 0x0080) {
  258. /* A possible match in comp_table_data. We have to buffer it. */
  259. /* We know it's a JISX 0213 plane 1 character. */
  260. if (jch & 0x8000) abort();
  261. conv->ostate = (s1 << 8) | s2;
  262. return count+0;
  263. }
  264. /* Output the shifted representation. */
  265. if (n >= count+2) {
  266. r[0] = s1;
  267. r[1] = s2;
  268. conv->ostate = 0;
  269. return count+2;
  270. } else
  271. return RET_TOOSMALL;
  272. }
  273. return RET_ILUNI;
  274. }
  275. }
  276. static int
  277. shift_jisx0213_reset (conv_t conv, unsigned char *r, size_t n)
  278. {
  279. state_t lasttwo = conv->ostate;
  280. if (lasttwo) {
  281. if (n < 2)
  282. return RET_TOOSMALL;
  283. r[0] = (lasttwo >> 8) & 0xff;
  284. r[1] = lasttwo & 0xff;
  285. /* conv->ostate = 0; will be done by the caller */
  286. return 2;
  287. } else
  288. return 0;
  289. }