iso2022_jp1.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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-1
  22. */
  23. /* Specification: RFC 2237 */
  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. #define STATE_JISX0212 3
  32. static int
  33. iso2022_jp1_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  34. {
  35. state_t state = conv->istate;
  36. int count = 0;
  37. unsigned char c;
  38. for (;;) {
  39. c = *s;
  40. if (c == ESC) {
  41. if (n < count+3)
  42. goto none;
  43. if (s[1] == '(') {
  44. if (s[2] == 'B') {
  45. state = STATE_ASCII;
  46. s += 3; count += 3;
  47. if (n < count+1)
  48. goto none;
  49. continue;
  50. }
  51. if (s[2] == 'J') {
  52. state = STATE_JISX0201ROMAN;
  53. s += 3; count += 3;
  54. if (n < count+1)
  55. goto none;
  56. continue;
  57. }
  58. goto ilseq;
  59. }
  60. if (s[1] == '$') {
  61. if (s[2] == '@' || s[2] == 'B') {
  62. /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
  63. state = STATE_JISX0208;
  64. s += 3; count += 3;
  65. if (n < count+1)
  66. goto none;
  67. continue;
  68. }
  69. if (s[2] == '(') {
  70. if (n < count+4)
  71. goto none;
  72. if (s[3] == 'D') {
  73. state = STATE_JISX0212;
  74. s += 4; count += 4;
  75. if (n < count+1)
  76. goto none;
  77. continue;
  78. }
  79. }
  80. goto ilseq;
  81. }
  82. goto ilseq;
  83. }
  84. break;
  85. }
  86. switch (state) {
  87. case STATE_ASCII:
  88. if (c < 0x80) {
  89. int ret = ascii_mbtowc(conv,pwc,s,1);
  90. if (ret == RET_ILSEQ)
  91. goto ilseq;
  92. if (ret != 1) abort();
  93. conv->istate = state;
  94. return count+1;
  95. } else
  96. goto ilseq;
  97. case STATE_JISX0201ROMAN:
  98. if (c < 0x80) {
  99. int ret = jisx0201_mbtowc(conv,pwc,s,1);
  100. if (ret == RET_ILSEQ)
  101. goto ilseq;
  102. if (ret != 1) abort();
  103. conv->istate = state;
  104. return count+1;
  105. } else
  106. goto ilseq;
  107. case STATE_JISX0208:
  108. if (n < count+2)
  109. goto none;
  110. if (s[0] < 0x80 && s[1] < 0x80) {
  111. int ret = jisx0208_mbtowc(conv,pwc,s,2);
  112. if (ret == RET_ILSEQ)
  113. goto ilseq;
  114. if (ret != 2) abort();
  115. conv->istate = state;
  116. return count+2;
  117. } else
  118. goto ilseq;
  119. case STATE_JISX0212:
  120. if (n < count+2)
  121. goto none;
  122. if (s[0] < 0x80 && s[1] < 0x80) {
  123. int ret = jisx0212_mbtowc(conv,pwc,s,2);
  124. if (ret == RET_ILSEQ)
  125. goto ilseq;
  126. if (ret != 2) abort();
  127. conv->istate = state;
  128. return count+2;
  129. } else
  130. goto ilseq;
  131. default: abort();
  132. }
  133. none:
  134. conv->istate = state;
  135. return RET_TOOFEW(count);
  136. ilseq:
  137. conv->istate = state;
  138. return RET_SHIFT_ILSEQ(count);
  139. }
  140. static int
  141. iso2022_jp1_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  142. {
  143. state_t state = conv->ostate;
  144. unsigned char buf[2];
  145. int ret;
  146. /* Try ASCII. */
  147. ret = ascii_wctomb(conv,buf,wc,1);
  148. if (ret != RET_ILUNI) {
  149. if (ret != 1) abort();
  150. if (buf[0] < 0x80) {
  151. int count = (state == STATE_ASCII ? 1 : 4);
  152. if (n < count)
  153. return RET_TOOSMALL;
  154. if (state != STATE_ASCII) {
  155. r[0] = ESC;
  156. r[1] = '(';
  157. r[2] = 'B';
  158. r += 3;
  159. state = STATE_ASCII;
  160. }
  161. r[0] = buf[0];
  162. conv->ostate = state;
  163. return count;
  164. }
  165. }
  166. /* Try JIS X 0201-1976 Roman. */
  167. ret = jisx0201_wctomb(conv,buf,wc,1);
  168. if (ret != RET_ILUNI) {
  169. if (ret != 1) abort();
  170. if (buf[0] < 0x80) {
  171. int count = (state == STATE_JISX0201ROMAN ? 1 : 4);
  172. if (n < count)
  173. return RET_TOOSMALL;
  174. if (state != STATE_JISX0201ROMAN) {
  175. r[0] = ESC;
  176. r[1] = '(';
  177. r[2] = 'J';
  178. r += 3;
  179. state = STATE_JISX0201ROMAN;
  180. }
  181. r[0] = buf[0];
  182. conv->ostate = state;
  183. return count;
  184. }
  185. }
  186. /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
  187. ret = jisx0208_wctomb(conv,buf,wc,2);
  188. if (ret != RET_ILUNI) {
  189. if (ret != 2) abort();
  190. if (buf[0] < 0x80 && buf[1] < 0x80) {
  191. int count = (state == STATE_JISX0208 ? 2 : 5);
  192. if (n < count)
  193. return RET_TOOSMALL;
  194. if (state != STATE_JISX0208) {
  195. r[0] = ESC;
  196. r[1] = '$';
  197. r[2] = 'B';
  198. r += 3;
  199. state = STATE_JISX0208;
  200. }
  201. r[0] = buf[0];
  202. r[1] = buf[1];
  203. conv->ostate = state;
  204. return count;
  205. }
  206. }
  207. /* Try JIS X 0212-1990. */
  208. ret = jisx0212_wctomb(conv,buf,wc,2);
  209. if (ret != RET_ILUNI) {
  210. if (ret != 2) abort();
  211. if (buf[0] < 0x80 && buf[1] < 0x80) {
  212. int count = (state == STATE_JISX0212 ? 2 : 6);
  213. if (n < count)
  214. return RET_TOOSMALL;
  215. if (state != STATE_JISX0212) {
  216. r[0] = ESC;
  217. r[1] = '$';
  218. r[2] = '(';
  219. r[3] = 'D';
  220. r += 4;
  221. state = STATE_JISX0212;
  222. }
  223. r[0] = buf[0];
  224. r[1] = buf[1];
  225. conv->ostate = state;
  226. return count;
  227. }
  228. }
  229. return RET_ILUNI;
  230. }
  231. static int
  232. iso2022_jp1_reset (conv_t conv, unsigned char *r, int n)
  233. {
  234. state_t state = conv->ostate;
  235. if (state != STATE_ASCII) {
  236. if (n < 3)
  237. return RET_TOOSMALL;
  238. r[0] = ESC;
  239. r[1] = '(';
  240. r[2] = 'B';
  241. /* conv->ostate = 0; will be done by the caller */
  242. return 3;
  243. } else
  244. return 0;
  245. }
  246. #undef STATE_JISX0212
  247. #undef STATE_JISX0208
  248. #undef STATE_JISX0201ROMAN
  249. #undef STATE_ASCII