iso2022_jp.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 1999-2001, 2008 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. * ISO-2022-JP
  22. */
  23. /* Specification: RFC 1468 */
  24. #define ESC 0x1b
  25. /*
  26. * The state can be one of the following values.
  27. */
  28. #define STATE_ASCII 0
  29. #define STATE_JISX0201ROMAN 1
  30. #define STATE_JISX0208 2
  31. static int
  32. iso2022_jp_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  33. {
  34. state_t state = conv->istate;
  35. int count = 0;
  36. unsigned char c;
  37. for (;;) {
  38. c = *s;
  39. if (c == ESC) {
  40. if (n < count+3)
  41. goto none;
  42. if (s[1] == '(') {
  43. if (s[2] == 'B') {
  44. state = STATE_ASCII;
  45. s += 3; count += 3;
  46. if (n < count+1)
  47. goto none;
  48. continue;
  49. }
  50. if (s[2] == 'J') {
  51. state = STATE_JISX0201ROMAN;
  52. s += 3; count += 3;
  53. if (n < count+1)
  54. goto none;
  55. continue;
  56. }
  57. goto ilseq;
  58. }
  59. if (s[1] == '$') {
  60. if (s[2] == '@' || s[2] == 'B') {
  61. /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
  62. state = STATE_JISX0208;
  63. s += 3; count += 3;
  64. if (n < count+1)
  65. goto none;
  66. continue;
  67. }
  68. goto ilseq;
  69. }
  70. goto ilseq;
  71. }
  72. break;
  73. }
  74. switch (state) {
  75. case STATE_ASCII:
  76. if (c < 0x80) {
  77. int ret = ascii_mbtowc(conv,pwc,s,1);
  78. if (ret == RET_ILSEQ)
  79. goto ilseq;
  80. if (ret != 1) abort();
  81. conv->istate = state;
  82. return count+1;
  83. } else
  84. goto ilseq;
  85. case STATE_JISX0201ROMAN:
  86. if (c < 0x80) {
  87. int ret = jisx0201_mbtowc(conv,pwc,s,1);
  88. if (ret == RET_ILSEQ)
  89. goto ilseq;
  90. if (ret != 1) abort();
  91. conv->istate = state;
  92. return count+1;
  93. } else
  94. goto ilseq;
  95. case STATE_JISX0208:
  96. if (n < count+2)
  97. goto none;
  98. if (s[0] < 0x80 && s[1] < 0x80) {
  99. int ret = jisx0208_mbtowc(conv,pwc,s,2);
  100. if (ret == RET_ILSEQ)
  101. goto ilseq;
  102. if (ret != 2) abort();
  103. conv->istate = state;
  104. return count+2;
  105. } else
  106. goto ilseq;
  107. default: abort();
  108. }
  109. none:
  110. conv->istate = state;
  111. return RET_TOOFEW(count);
  112. ilseq:
  113. conv->istate = state;
  114. return RET_SHIFT_ILSEQ(count);
  115. }
  116. static int
  117. iso2022_jp_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  118. {
  119. state_t state = conv->ostate;
  120. unsigned char buf[2];
  121. int ret;
  122. /* Try ASCII. */
  123. ret = ascii_wctomb(conv,buf,wc,1);
  124. if (ret != RET_ILUNI) {
  125. if (ret != 1) abort();
  126. if (buf[0] < 0x80) {
  127. int count = (state == STATE_ASCII ? 1 : 4);
  128. if (n < count)
  129. return RET_TOOSMALL;
  130. if (state != STATE_ASCII) {
  131. r[0] = ESC;
  132. r[1] = '(';
  133. r[2] = 'B';
  134. r += 3;
  135. state = STATE_ASCII;
  136. }
  137. r[0] = buf[0];
  138. conv->ostate = state;
  139. return count;
  140. }
  141. }
  142. /* Try JIS X 0201-1976 Roman. */
  143. ret = jisx0201_wctomb(conv,buf,wc,1);
  144. if (ret != RET_ILUNI) {
  145. if (ret != 1) abort();
  146. if (buf[0] < 0x80) {
  147. int count = (state == STATE_JISX0201ROMAN ? 1 : 4);
  148. if (n < count)
  149. return RET_TOOSMALL;
  150. if (state != STATE_JISX0201ROMAN) {
  151. r[0] = ESC;
  152. r[1] = '(';
  153. r[2] = 'J';
  154. r += 3;
  155. state = STATE_JISX0201ROMAN;
  156. }
  157. r[0] = buf[0];
  158. conv->ostate = state;
  159. return count;
  160. }
  161. }
  162. /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
  163. ret = jisx0208_wctomb(conv,buf,wc,2);
  164. if (ret != RET_ILUNI) {
  165. if (ret != 2) abort();
  166. if (buf[0] < 0x80 && buf[1] < 0x80) {
  167. int count = (state == STATE_JISX0208 ? 2 : 5);
  168. if (n < count)
  169. return RET_TOOSMALL;
  170. if (state != STATE_JISX0208) {
  171. r[0] = ESC;
  172. r[1] = '$';
  173. r[2] = 'B';
  174. r += 3;
  175. state = STATE_JISX0208;
  176. }
  177. r[0] = buf[0];
  178. r[1] = buf[1];
  179. conv->ostate = state;
  180. return count;
  181. }
  182. }
  183. return RET_ILUNI;
  184. }
  185. static int
  186. iso2022_jp_reset (conv_t conv, unsigned char *r, int n)
  187. {
  188. state_t state = conv->ostate;
  189. if (state != STATE_ASCII) {
  190. if (n < 3)
  191. return RET_TOOSMALL;
  192. r[0] = ESC;
  193. r[1] = '(';
  194. r[2] = 'B';
  195. /* conv->ostate = 0; will be done by the caller */
  196. return 3;
  197. } else
  198. return 0;
  199. }
  200. #undef STATE_JISX0208
  201. #undef STATE_JISX0201ROMAN
  202. #undef STATE_ASCII