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