iso2022_jp.h 5.1 KB

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