euc_jisx0213.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * EUC-JISX0213
  22. */
  23. /* The structure of EUC-JISX0213 is as follows:
  24. 0x00..0x7F: ASCII
  25. 0x8E{A1..FE}: JISX0201 Katakana, with prefix 0x8E, offset by +0x80.
  26. 0x8F{A1..FE}{A1..FE}: JISX0213 plane 2, with prefix 0x8F, offset by +0x8080.
  27. 0x{A1..FE}{A1..FE}: JISX0213 plane 1, offset by +0x8080.
  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. euc_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 ASCII character. */
  46. *pwc = (ucs4_t) c;
  47. return 1;
  48. } else {
  49. if ((c >= 0xa1 && c <= 0xfe) || c == 0x8e || c == 0x8f) {
  50. /* Two or three byte character. */
  51. if (n >= 2) {
  52. unsigned char c2 = s[1];
  53. if (c2 >= 0xa1 && c2 <= 0xfe) {
  54. if (c == 0x8e) {
  55. /* Half-width katakana. */
  56. if (c2 <= 0xdf) {
  57. *pwc = c2 + 0xfec0;
  58. return 2;
  59. }
  60. } else {
  61. ucs4_t wc;
  62. if (c == 0x8f) {
  63. /* JISX 0213 plane 2. */
  64. if (n >= 3) {
  65. unsigned char c3 = s[2];
  66. wc = jisx0213_to_ucs4(0x200-0x80+c2,c3^0x80);
  67. } else
  68. return RET_TOOFEW(0);
  69. } else {
  70. /* JISX 0213 plane 1. */
  71. wc = jisx0213_to_ucs4(0x100-0x80+c,c2^0x80);
  72. }
  73. if (wc) {
  74. if (wc < 0x80) {
  75. /* It's a combining character. */
  76. ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
  77. ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
  78. /* We cannot output two Unicode characters at once. So,
  79. output the first character and buffer the second one. */
  80. *pwc = wc1;
  81. conv->istate = wc2;
  82. } else
  83. *pwc = wc;
  84. return (c == 0x8f ? 3 : 2);
  85. }
  86. }
  87. }
  88. } else
  89. return RET_TOOFEW(0);
  90. }
  91. return RET_ILSEQ;
  92. }
  93. }
  94. }
  95. #define euc_jisx0213_flushwc normal_flushwc
  96. /* Composition tables for each of the relevant combining characters. */
  97. static const struct { unsigned short base; unsigned short composed; } euc_jisx0213_comp_table_data[] = {
  98. #define euc_jisx0213_comp_table02e5_idx 0
  99. #define euc_jisx0213_comp_table02e5_len 1
  100. { 0xabe4, 0xabe5 }, /* 0x12B65 = 0x12B64 U+02E5 */
  101. #define euc_jisx0213_comp_table02e9_idx (euc_jisx0213_comp_table02e5_idx+euc_jisx0213_comp_table02e5_len)
  102. #define euc_jisx0213_comp_table02e9_len 1
  103. { 0xabe0, 0xabe6 }, /* 0x12B66 = 0x12B60 U+02E9 */
  104. #define euc_jisx0213_comp_table0300_idx (euc_jisx0213_comp_table02e9_idx+euc_jisx0213_comp_table02e9_len)
  105. #define euc_jisx0213_comp_table0300_len 5
  106. { 0xa9dc, 0xabc4 }, /* 0x12B44 = 0x1295C U+0300 */
  107. { 0xabb8, 0xabc8 }, /* 0x12B48 = 0x12B38 U+0300 */
  108. { 0xabb7, 0xabca }, /* 0x12B4A = 0x12B37 U+0300 */
  109. { 0xabb0, 0xabcc }, /* 0x12B4C = 0x12B30 U+0300 */
  110. { 0xabc3, 0xabce }, /* 0x12B4E = 0x12B43 U+0300 */
  111. #define euc_jisx0213_comp_table0301_idx (euc_jisx0213_comp_table0300_idx+euc_jisx0213_comp_table0300_len)
  112. #define euc_jisx0213_comp_table0301_len 4
  113. { 0xabb8, 0xabc9 }, /* 0x12B49 = 0x12B38 U+0301 */
  114. { 0xabb7, 0xabcb }, /* 0x12B4B = 0x12B37 U+0301 */
  115. { 0xabb0, 0xabcd }, /* 0x12B4D = 0x12B30 U+0301 */
  116. { 0xabc3, 0xabcf }, /* 0x12B4F = 0x12B43 U+0301 */
  117. #define euc_jisx0213_comp_table309a_idx (euc_jisx0213_comp_table0301_idx+euc_jisx0213_comp_table0301_len)
  118. #define euc_jisx0213_comp_table309a_len 14
  119. { 0xa4ab, 0xa4f7 }, /* 0x12477 = 0x1242B U+309A */
  120. { 0xa4ad, 0xa4f8 }, /* 0x12478 = 0x1242D U+309A */
  121. { 0xa4af, 0xa4f9 }, /* 0x12479 = 0x1242F U+309A */
  122. { 0xa4b1, 0xa4fa }, /* 0x1247A = 0x12431 U+309A */
  123. { 0xa4b3, 0xa4fb }, /* 0x1247B = 0x12433 U+309A */
  124. { 0xa5ab, 0xa5f7 }, /* 0x12577 = 0x1252B U+309A */
  125. { 0xa5ad, 0xa5f8 }, /* 0x12578 = 0x1252D U+309A */
  126. { 0xa5af, 0xa5f9 }, /* 0x12579 = 0x1252F U+309A */
  127. { 0xa5b1, 0xa5fa }, /* 0x1257A = 0x12531 U+309A */
  128. { 0xa5b3, 0xa5fb }, /* 0x1257B = 0x12533 U+309A */
  129. { 0xa5bb, 0xa5fc }, /* 0x1257C = 0x1253B U+309A */
  130. { 0xa5c4, 0xa5fd }, /* 0x1257D = 0x12544 U+309A */
  131. { 0xa5c8, 0xa5fe }, /* 0x1257E = 0x12548 U+309A */
  132. { 0xa6f5, 0xa6f8 }, /* 0x12678 = 0x12675 U+309A */
  133. };
  134. static int
  135. euc_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  136. {
  137. int count = 0;
  138. unsigned short lasttwo = conv->ostate;
  139. if (lasttwo) {
  140. /* Attempt to combine the last character with this one. */
  141. unsigned int idx;
  142. unsigned int len;
  143. if (wc == 0x02e5)
  144. idx = euc_jisx0213_comp_table02e5_idx,
  145. len = euc_jisx0213_comp_table02e5_len;
  146. else if (wc == 0x02e9)
  147. idx = euc_jisx0213_comp_table02e9_idx,
  148. len = euc_jisx0213_comp_table02e9_len;
  149. else if (wc == 0x0300)
  150. idx = euc_jisx0213_comp_table0300_idx,
  151. len = euc_jisx0213_comp_table0300_len;
  152. else if (wc == 0x0301)
  153. idx = euc_jisx0213_comp_table0301_idx,
  154. len = euc_jisx0213_comp_table0301_len;
  155. else if (wc == 0x309a)
  156. idx = euc_jisx0213_comp_table309a_idx,
  157. len = euc_jisx0213_comp_table309a_len;
  158. else
  159. goto not_combining;
  160. do
  161. if (euc_jisx0213_comp_table_data[idx].base == lasttwo)
  162. break;
  163. while (++idx, --len > 0);
  164. if (len > 0) {
  165. /* Output the combined character. */
  166. if (n >= 2) {
  167. lasttwo = euc_jisx0213_comp_table_data[idx].composed;
  168. r[0] = (lasttwo >> 8) & 0xff;
  169. r[1] = lasttwo & 0xff;
  170. conv->ostate = 0;
  171. return 2;
  172. } else
  173. return RET_TOOSMALL;
  174. }
  175. not_combining:
  176. /* Output the buffered character. */
  177. if (n < 2)
  178. return RET_TOOSMALL;
  179. r[0] = (lasttwo >> 8) & 0xff;
  180. r[1] = lasttwo & 0xff;
  181. r += 2;
  182. count = 2;
  183. }
  184. if (wc < 0x80) {
  185. /* Plain ASCII character. */
  186. if (n > count) {
  187. r[0] = (unsigned char) wc;
  188. conv->ostate = 0;
  189. return count+1;
  190. } else
  191. return RET_TOOSMALL;
  192. } else if (wc >= 0xff61 && wc <= 0xff9f) {
  193. /* Half-width katakana. */
  194. if (n >= count+2) {
  195. r[0] = 0x8e;
  196. r[1] = wc - 0xfec0;
  197. conv->ostate = 0;
  198. return count+2;
  199. } else
  200. return RET_TOOSMALL;
  201. } else {
  202. unsigned short jch = ucs4_to_jisx0213(wc);
  203. if (jch != 0) {
  204. if (jch & 0x0080) {
  205. /* A possible match in comp_table_data. We have to buffer it. */
  206. /* We know it's a JISX 0213 plane 1 character. */
  207. if (jch & 0x8000) abort();
  208. conv->ostate = jch | 0x8080;
  209. return count+0;
  210. }
  211. if (jch & 0x8000) {
  212. /* JISX 0213 plane 2. */
  213. if (n >= count+3) {
  214. r[0] = 0x8f;
  215. r[1] = (jch >> 8) | 0x80;
  216. r[2] = (jch & 0xff) | 0x80;
  217. conv->ostate = 0;
  218. return count+3;
  219. } else
  220. return RET_TOOSMALL;
  221. } else {
  222. /* JISX 0213 plane 1. */
  223. if (n >= count+2) {
  224. r[0] = (jch >> 8) | 0x80;
  225. r[1] = (jch & 0xff) | 0x80;
  226. conv->ostate = 0;
  227. return count+2;
  228. } else
  229. return RET_TOOSMALL;
  230. }
  231. }
  232. return RET_ILUNI;
  233. }
  234. }
  235. static int
  236. euc_jisx0213_reset (conv_t conv, unsigned char *r, int n)
  237. {
  238. state_t lasttwo = conv->ostate;
  239. if (lasttwo) {
  240. if (n < 2)
  241. return RET_TOOSMALL;
  242. r[0] = (lasttwo >> 8) & 0xff;
  243. r[1] = lasttwo & 0xff;
  244. /* conv->ostate = 0; will be done by the caller */
  245. return 2;
  246. } else
  247. return 0;
  248. }