iso2022_kr.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-KR
  21. */
  22. /* Specification: RFC 1557 */
  23. /* Note: CJK.INF says the SO designator needs to appear only once at the
  24. beginning of a text, but to decrease the risk of ambiguities, when
  25. producing ISO-2022-KR, we repeat the designator in every line containing
  26. SO characters. RFC 1557 does not mandate this. */
  27. #define ESC 0x1b
  28. #define SO 0x0e
  29. #define SI 0x0f
  30. /*
  31. * The state is composed of one of the following values
  32. */
  33. #define STATE_ASCII 0
  34. #define STATE_TWOBYTE 1
  35. /*
  36. * and one of the following values, << 8
  37. */
  38. #define STATE2_NONE 0
  39. #define STATE2_DESIGNATED_KSC5601 1
  40. #define SPLIT_STATE \
  41. unsigned int state1 = state & 0xff, state2 = state >> 8
  42. #define COMBINE_STATE \
  43. state = (state2 << 8) | state1
  44. static int
  45. iso2022_kr_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  46. {
  47. state_t state = conv->istate;
  48. SPLIT_STATE;
  49. int count = 0;
  50. unsigned char c;
  51. for (;;) {
  52. c = *s;
  53. if (c == ESC) {
  54. if (n < count+4)
  55. goto none;
  56. if (s[1] == '$') {
  57. if (s[2] == ')') {
  58. if (s[3] == 'C') {
  59. state2 = STATE2_DESIGNATED_KSC5601;
  60. s += 4; count += 4;
  61. if (n < count+1)
  62. goto none;
  63. continue;
  64. }
  65. }
  66. }
  67. goto ilseq;
  68. }
  69. if (c == SO) {
  70. if (state2 != STATE2_DESIGNATED_KSC5601)
  71. goto ilseq;
  72. state1 = STATE_TWOBYTE;
  73. s++; count++;
  74. if (n < count+1)
  75. goto none;
  76. continue;
  77. }
  78. if (c == SI) {
  79. state1 = STATE_ASCII;
  80. s++; count++;
  81. if (n < count+1)
  82. goto none;
  83. continue;
  84. }
  85. break;
  86. }
  87. switch (state1) {
  88. case STATE_ASCII:
  89. if (c < 0x80) {
  90. int ret = ascii_mbtowc(conv,pwc,s,1);
  91. if (ret == RET_ILSEQ)
  92. goto ilseq;
  93. if (ret != 1) abort();
  94. #if 0 /* Accept ISO-2022-KR according to CJK.INF. */
  95. if (*pwc == 0x000a || *pwc == 0x000d)
  96. state2 = STATE2_NONE;
  97. #endif
  98. COMBINE_STATE;
  99. conv->istate = state;
  100. return count+1;
  101. } else
  102. goto ilseq;
  103. case STATE_TWOBYTE:
  104. if (n < count+2)
  105. goto none;
  106. if (state2 != STATE2_DESIGNATED_KSC5601) abort();
  107. if (s[0] < 0x80 && s[1] < 0x80) {
  108. int ret = ksc5601_mbtowc(conv,pwc,s,2);
  109. if (ret == RET_ILSEQ)
  110. goto ilseq;
  111. if (ret != 2) abort();
  112. COMBINE_STATE;
  113. conv->istate = state;
  114. return count+2;
  115. } else
  116. goto ilseq;
  117. default: abort();
  118. }
  119. none:
  120. COMBINE_STATE;
  121. conv->istate = state;
  122. return RET_TOOFEW(count);
  123. ilseq:
  124. COMBINE_STATE;
  125. conv->istate = state;
  126. return RET_SHIFT_ILSEQ(count);
  127. }
  128. static int
  129. iso2022_kr_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  130. {
  131. state_t state = conv->ostate;
  132. SPLIT_STATE;
  133. unsigned char buf[2];
  134. int ret;
  135. /* Try ASCII. */
  136. ret = ascii_wctomb(conv,buf,wc,1);
  137. if (ret != RET_ILUNI) {
  138. if (ret != 1) abort();
  139. if (buf[0] < 0x80) {
  140. int count = (state1 == STATE_ASCII ? 1 : 2);
  141. if (n < count)
  142. return RET_TOOSMALL;
  143. if (state1 != STATE_ASCII) {
  144. r[0] = SI;
  145. r += 1;
  146. state1 = STATE_ASCII;
  147. }
  148. r[0] = buf[0];
  149. if (wc == 0x000a || wc == 0x000d)
  150. state2 = STATE2_NONE;
  151. COMBINE_STATE;
  152. conv->ostate = state;
  153. return count;
  154. }
  155. }
  156. /* Try KS C 5601-1992. */
  157. ret = ksc5601_wctomb(conv,buf,wc,2);
  158. if (ret != RET_ILUNI) {
  159. if (ret != 2) abort();
  160. if (buf[0] < 0x80 && buf[1] < 0x80) {
  161. int count = (state2 == STATE2_DESIGNATED_KSC5601 ? 0 : 4) + (state1 == STATE_TWOBYTE ? 0 : 1) + 2;
  162. if (n < count)
  163. return RET_TOOSMALL;
  164. if (state2 != STATE2_DESIGNATED_KSC5601) {
  165. r[0] = ESC;
  166. r[1] = '$';
  167. r[2] = ')';
  168. r[3] = 'C';
  169. r += 4;
  170. state2 = STATE2_DESIGNATED_KSC5601;
  171. }
  172. if (state1 != STATE_TWOBYTE) {
  173. r[0] = SO;
  174. r += 1;
  175. state1 = STATE_TWOBYTE;
  176. }
  177. r[0] = buf[0];
  178. r[1] = buf[1];
  179. COMBINE_STATE;
  180. conv->ostate = state;
  181. return count;
  182. }
  183. }
  184. return RET_ILUNI;
  185. }
  186. static int
  187. iso2022_kr_reset (conv_t conv, unsigned char *r, size_t n)
  188. {
  189. state_t state = conv->ostate;
  190. SPLIT_STATE;
  191. (void)state2;
  192. if (state1 != STATE_ASCII) {
  193. if (n < 1)
  194. return RET_TOOSMALL;
  195. r[0] = SI;
  196. /* conv->ostate = 0; will be done by the caller */
  197. return 1;
  198. } else
  199. return 0;
  200. }
  201. #undef COMBINE_STATE
  202. #undef SPLIT_STATE
  203. #undef STATE2_DESIGNATED_KSC5601
  204. #undef STATE2_NONE
  205. #undef STATE_TWOBYTE
  206. #undef STATE_ASCII