euc_jp.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 1999-2001, 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. * EUC-JP
  21. */
  22. static int
  23. euc_jp_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  24. {
  25. unsigned char c = *s;
  26. /* Code set 0 (ASCII or JIS X 0201-1976 Roman) */
  27. if (c < 0x80)
  28. return ascii_mbtowc(conv,pwc,s,n);
  29. /* Code set 1 (JIS X 0208) */
  30. if (c >= 0xa1 && c < 0xff) {
  31. if (n < 2)
  32. return RET_TOOFEW(0);
  33. if (c < 0xf5) {
  34. unsigned char c2 = s[1];
  35. if (c2 >= 0xa1 && c2 < 0xff) {
  36. unsigned char buf[2];
  37. buf[0] = c-0x80; buf[1] = c2-0x80;
  38. return jisx0208_mbtowc(conv,pwc,buf,2);
  39. } else
  40. return RET_ILSEQ;
  41. } else {
  42. /* User-defined range. See
  43. * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  44. unsigned char c2 = s[1];
  45. if (c2 >= 0xa1 && c2 < 0xff) {
  46. *pwc = 0xe000 + 94*(c-0xf5) + (c2-0xa1);
  47. return 2;
  48. } else
  49. return RET_ILSEQ;
  50. }
  51. }
  52. /* Code set 2 (half-width katakana) */
  53. if (c == 0x8e) {
  54. if (n < 2)
  55. return RET_TOOFEW(0);
  56. {
  57. unsigned char c2 = s[1];
  58. if (c2 >= 0xa1 && c2 < 0xe0) {
  59. int ret = jisx0201_mbtowc(conv,pwc,s+1,n-1);
  60. if (ret == RET_ILSEQ)
  61. return RET_ILSEQ;
  62. if (ret != 1) abort();
  63. return 2;
  64. } else
  65. return RET_ILSEQ;
  66. }
  67. }
  68. /* Code set 3 (JIS X 0212-1990) */
  69. if (c == 0x8f) {
  70. if (n < 2)
  71. return RET_TOOFEW(0);
  72. {
  73. unsigned char c2 = s[1];
  74. if (c2 >= 0xa1 && c2 < 0xff) {
  75. if (n < 3)
  76. return RET_TOOFEW(0);
  77. if (c2 < 0xf5) {
  78. unsigned char c3 = s[2];
  79. if (c3 >= 0xa1 && c3 < 0xff) {
  80. unsigned char buf[2];
  81. int ret;
  82. buf[0] = c2-0x80; buf[1] = c3-0x80;
  83. ret = jisx0212_mbtowc(conv,pwc,buf,2);
  84. if (ret == RET_ILSEQ)
  85. return RET_ILSEQ;
  86. if (ret != 2) abort();
  87. return 3;
  88. } else
  89. return RET_ILSEQ;
  90. } else {
  91. /* User-defined range. See
  92. * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  93. unsigned char c3 = s[2];
  94. if (c3 >= 0xa1 && c3 < 0xff) {
  95. *pwc = 0xe3ac + 94*(c2-0xf5) + (c3-0xa1);
  96. return 3;
  97. } else
  98. return RET_ILSEQ;
  99. }
  100. } else
  101. return RET_ILSEQ;
  102. }
  103. }
  104. return RET_ILSEQ;
  105. }
  106. static int
  107. euc_jp_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  108. {
  109. unsigned char buf[2];
  110. int ret;
  111. /* Code set 0 (ASCII or JIS X 0201-1976 Roman) */
  112. ret = ascii_wctomb(conv,r,wc,n);
  113. if (ret != RET_ILUNI)
  114. return ret;
  115. /* Code set 1 (JIS X 0208) */
  116. ret = jisx0208_wctomb(conv,buf,wc,2);
  117. if (ret != RET_ILUNI) {
  118. if (ret != 2) abort();
  119. if (n < 2)
  120. return RET_TOOSMALL;
  121. r[0] = buf[0]+0x80;
  122. r[1] = buf[1]+0x80;
  123. return 2;
  124. }
  125. /* Code set 2 (half-width katakana) */
  126. ret = jisx0201_wctomb(conv,buf,wc,1);
  127. if (ret != RET_ILUNI && buf[0] >= 0x80) {
  128. if (ret != 1) abort();
  129. if (n < 2)
  130. return RET_TOOSMALL;
  131. r[0] = 0x8e;
  132. r[1] = buf[0];
  133. return 2;
  134. }
  135. /* Code set 3 (JIS X 0212-1990) */
  136. ret = jisx0212_wctomb(conv,buf,wc,2);
  137. if (ret != RET_ILUNI) {
  138. if (ret != 2) abort();
  139. if (n < 3)
  140. return RET_TOOSMALL;
  141. r[0] = 0x8f;
  142. r[1] = buf[0]+0x80;
  143. r[2] = buf[1]+0x80;
  144. return 3;
  145. }
  146. /* Extra compatibility with Shift_JIS. */
  147. if (wc == 0x00a5) {
  148. r[0] = 0x5c;
  149. return 1;
  150. }
  151. if (wc == 0x203e) {
  152. r[0] = 0x7e;
  153. return 1;
  154. }
  155. /* User-defined range. See
  156. * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  157. if (wc >= 0xe000 && wc < 0xe758) {
  158. if (wc < 0xe3ac) {
  159. unsigned char c1, c2;
  160. if (n < 2)
  161. return RET_TOOSMALL;
  162. c1 = (unsigned int) (wc - 0xe000) / 94;
  163. c2 = (unsigned int) (wc - 0xe000) % 94;
  164. r[0] = c1+0xf5;
  165. r[1] = c2+0xa1;
  166. return 2;
  167. } else {
  168. unsigned char c1, c2;
  169. if (n < 3)
  170. return RET_TOOSMALL;
  171. c1 = (unsigned int) (wc - 0xe3ac) / 94;
  172. c2 = (unsigned int) (wc - 0xe3ac) % 94;
  173. r[0] = 0x8f;
  174. r[1] = c1+0xf5;
  175. r[2] = c2+0xa1;
  176. return 3;
  177. }
  178. }
  179. return RET_ILUNI;
  180. }