sjis.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 1999-2002 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. * SHIFT_JIS
  22. */
  23. /*
  24. Conversion between SJIS codes (s1,s2) and JISX0208 codes (c1,c2):
  25. Example. (s1,s2) = 0x8140, (c1,c2) = 0x2121.
  26. 0x81 <= s1 <= 0x9F || 0xE0 <= s1 <= 0xEA,
  27. 0x40 <= s2 <= 0x7E || 0x80 <= s2 <= 0xFC,
  28. 0x21 <= c1 <= 0x74, 0x21 <= c2 <= 0x7E.
  29. Invariant:
  30. 94*2*(s1 < 0xE0 ? s1-0x81 : s1-0xC1) + (s2 < 0x80 ? s2-0x40 : s2-0x41)
  31. = 94*(c1-0x21)+(c2-0x21)
  32. Conversion (s1,s2) -> (c1,c2):
  33. t1 := (s1 < 0xE0 ? s1-0x81 : s1-0xC1)
  34. t2 := (s2 < 0x80 ? s2-0x40 : s2-0x41)
  35. c1 := 2*t1 + (t2 < 0x5E ? 0 : 1) + 0x21
  36. c2 := (t2 < 0x5E ? t2 : t2-0x5E) + 0x21
  37. Conversion (c1,c2) -> (s1,s2):
  38. t1 := (c1 - 0x21) >> 1
  39. t2 := ((c1 - 0x21) & 1) * 0x5E + (c2 - 0x21)
  40. s1 := (t1 < 0x1F ? t1+0x81 : t1+0xC1)
  41. s2 := (t2 < 0x3F ? t2+0x40 : t2+0x41)
  42. */
  43. static int
  44. sjis_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  45. {
  46. unsigned char c = *s;
  47. if (c < 0x80 || (c >= 0xa1 && c <= 0xdf))
  48. return jisx0201_mbtowc(conv,pwc,s,n);
  49. else {
  50. unsigned char s1, s2;
  51. s1 = c;
  52. if ((s1 >= 0x81 && s1 <= 0x9f) || (s1 >= 0xe0 && s1 <= 0xea)) {
  53. if (n < 2)
  54. return RET_TOOFEW(0);
  55. s2 = s[1];
  56. if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) {
  57. unsigned char t1 = (s1 < 0xe0 ? s1-0x81 : s1-0xc1);
  58. unsigned char t2 = (s2 < 0x80 ? s2-0x40 : s2-0x41);
  59. unsigned char buf[2];
  60. buf[0] = 2*t1 + (t2 < 0x5e ? 0 : 1) + 0x21;
  61. buf[1] = (t2 < 0x5e ? t2 : t2-0x5e) + 0x21;
  62. return jisx0208_mbtowc(conv,pwc,buf,2);
  63. }
  64. } else if (s1 >= 0xf0 && s1 <= 0xf9) {
  65. /* User-defined range. See
  66. * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  67. if (n < 2)
  68. return RET_TOOFEW(0);
  69. s2 = s[1];
  70. if ((s2 >= 0x40 && s2 <= 0x7e) || (s2 >= 0x80 && s2 <= 0xfc)) {
  71. *pwc = 0xe000 + 188*(s1 - 0xf0) + (s2 < 0x80 ? s2-0x40 : s2-0x41);
  72. return 2;
  73. }
  74. }
  75. return RET_ILSEQ;
  76. }
  77. }
  78. static int
  79. sjis_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  80. {
  81. unsigned char buf[2];
  82. int ret;
  83. /* Try JIS X 0201-1976. */
  84. ret = jisx0201_wctomb(conv,buf,wc,1);
  85. if (ret != RET_ILUNI) {
  86. unsigned char c;
  87. if (ret != 1) abort();
  88. c = buf[0];
  89. if (c < 0x80 || (c >= 0xa1 && c <= 0xdf)) {
  90. r[0] = c;
  91. return 1;
  92. }
  93. }
  94. /* Try JIS X 0208-1990. */
  95. ret = jisx0208_wctomb(conv,buf,wc,2);
  96. if (ret != RET_ILUNI) {
  97. unsigned char c1, c2;
  98. if (ret != 2) abort();
  99. if (n < 2)
  100. return RET_TOOSMALL;
  101. c1 = buf[0];
  102. c2 = buf[1];
  103. if ((c1 >= 0x21 && c1 <= 0x74) && (c2 >= 0x21 && c2 <= 0x7e)) {
  104. unsigned char t1 = (c1 - 0x21) >> 1;
  105. unsigned char t2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21);
  106. r[0] = (t1 < 0x1f ? t1+0x81 : t1+0xc1);
  107. r[1] = (t2 < 0x3f ? t2+0x40 : t2+0x41);
  108. return 2;
  109. }
  110. }
  111. /* User-defined range. See
  112. * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
  113. if (wc >= 0xe000 && wc < 0xe758) {
  114. unsigned char c1, c2;
  115. if (n < 2)
  116. return RET_TOOSMALL;
  117. c1 = (unsigned int) (wc - 0xe000) / 188;
  118. c2 = (unsigned int) (wc - 0xe000) % 188;
  119. r[0] = c1+0xf0;
  120. r[1] = (c2 < 0x3f ? c2+0x40 : c2+0x41);
  121. return 2;
  122. }
  123. return RET_ILUNI;
  124. }