shift_jisx0213.h 9.6 KB

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