euc_jisx0213.h 8.5 KB

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