johab.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 1999-2001, 2007 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. * JOHAB
  22. */
  23. /*
  24. Conversion between JOHAB codes (s1,s2) and KSX1001 codes (c1,c2):
  25. Example. (s1,s2) = 0xD931, (c1,c2) = 0x2121.
  26. (s1,s2) = 0xDEF1, (c1,c2) = 0x2C71.
  27. (s1,s2) = 0xE031, (c1,c2) = 0x4A21.
  28. (s1,s2) = 0xF9FE, (c1,c2) = 0x7D7E.
  29. 0xD9 <= s1 <= 0xDE || 0xE0 <= s1 <= 0xF9,
  30. 0x31 <= s2 <= 0x7E || 0x91 <= s2 <= 0xFE,
  31. 0x21 <= c1 <= 0x2C || 0x4A <= c1 <= 0x7D,
  32. 0x21 <= c2 <= 0x7E.
  33. Invariant:
  34. 94*(s1 < 0xE0 ? 2*s1-0x1B2 : 2*s1-0x197) + (s2 < 0x91 ? s2-0x31 : s2-0x43)
  35. = 94*(c1-0x21)+(c2-0x21)
  36. Conversion (s1,s2) -> (c1,c2):
  37. t1 := (s1 < 0xE0 ? 2*s1-0x1B2 : 2*s1-0x197)
  38. t2 := (s2 < 0x91 ? s2-0x31 : s2-0x43)
  39. c1 := t1 + (t2 < 0x5E ? 0 : 1) + 0x21
  40. c2 := (t2 < 0x5E ? t2 : t2-0x5E) + 0x21
  41. Conversion (c1,c2) -> (s1,s2):
  42. t := (c1 < 0x4A ? (c1-0x21+0x1B2) : (c1-0x21+0x197))
  43. s1 := t >> 1
  44. t2 := (t & 1) * 0x5E + (c2 - 0x21)
  45. s2 := (t2 < 0x4E ? t2+0x31 : t2+0x43)
  46. */
  47. static int
  48. johab_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  49. {
  50. unsigned char c = *s;
  51. if (c < 0x80) {
  52. if (c == 0x5c)
  53. *pwc = (ucs4_t) 0x20a9;
  54. else
  55. *pwc = (ucs4_t) c;
  56. return 1;
  57. } else if (c < 0xd8) {
  58. return johab_hangul_mbtowc(conv,pwc,s,n);
  59. } else {
  60. unsigned char s1, s2;
  61. s1 = c;
  62. if ((s1 >= 0xd9 && s1 <= 0xde) || (s1 >= 0xe0 && s1 <= 0xf9)) {
  63. if (n < 2)
  64. return RET_TOOFEW(0);
  65. s2 = s[1];
  66. if ((s2 >= 0x31 && s2 <= 0x7e) || (s2 >= 0x91 && s2 <= 0xfe)) {
  67. /* In KSC 5601, now KS X 1001, the region s1 = 0xDA, 0xA1 <= s2 <= 0xD3
  68. contains the 51 Jamo (Hangul letters). But in the Johab encoding,
  69. they have been moved to the Hangul section, see
  70. johab_hangul_page31. */
  71. if (!(s1 == 0xda && (s2 >= 0xa1 && s2 <= 0xd3))) {
  72. unsigned char t1 = (s1 < 0xe0 ? 2*(s1-0xd9) : 2*s1-0x197);
  73. unsigned char t2 = (s2 < 0x91 ? s2-0x31 : s2-0x43);
  74. unsigned char buf[2];
  75. buf[0] = t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
  76. buf[1] = (t2 < 0x5e ? t2 : t2-0x5e) + 0x21;
  77. return ksc5601_mbtowc(conv,pwc,buf,2);
  78. }
  79. }
  80. }
  81. return RET_ILSEQ;
  82. }
  83. }
  84. static int
  85. johab_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  86. {
  87. unsigned char buf[2];
  88. int ret;
  89. /* Try ASCII variation. */
  90. if (wc < 0x0080 && wc != 0x005c) {
  91. *r = wc;
  92. return 1;
  93. }
  94. if (wc == 0x20a9) {
  95. *r = 0x5c;
  96. return 1;
  97. }
  98. /* Try JOHAB Hangul table before KSC5601 table, because the KSC5601 table
  99. contains some (2350 out of 11172) Hangul syllables (rows 0x30XX..0x48XX),
  100. and we want the search to return the JOHAB Hangul table entry. */
  101. /* Try JOHAB Hangul. */
  102. ret = johab_hangul_wctomb(conv,buf,wc,2);
  103. if (ret != RET_ILUNI) {
  104. if (ret != 2) abort();
  105. if (n < 2)
  106. return RET_TOOSMALL;
  107. r[0] = buf[0];
  108. r[1] = buf[1];
  109. return 2;
  110. }
  111. /* Try KSC5601, now KS X 1001. */
  112. ret = ksc5601_wctomb(conv,buf,wc,2);
  113. if (ret != RET_ILUNI) {
  114. unsigned char c1, c2;
  115. if (ret != 2) abort();
  116. if (n < 2)
  117. return RET_TOOSMALL;
  118. c1 = buf[0];
  119. c2 = buf[1];
  120. if (((c1 >= 0x21 && c1 <= 0x2c) || (c1 >= 0x4a && c1 <= 0x7d))
  121. && (c2 >= 0x21 && c2 <= 0x7e)) {
  122. unsigned int t = (c1 < 0x4A ? (c1-0x21+0x1B2) : (c1-0x21+0x197));
  123. unsigned char t2 = ((t & 1) ? 0x5e : 0) + (c2 - 0x21);
  124. r[0] = t >> 1;
  125. r[1] = (t2 < 0x4e ? t2+0x31 : t2+0x43);
  126. return 2;
  127. }
  128. }
  129. return RET_ILUNI;
  130. }