euc_jp.h 5.0 KB

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