iso2022_jp1.h 6.3 KB

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