1*aef5731fSOliver Tappe /*
2*aef5731fSOliver Tappe * Copyright (C) 1999-2001, 2005 Free Software Foundation, Inc.
3*aef5731fSOliver Tappe * This file is part of the GNU LIBICONV Library.
4*aef5731fSOliver Tappe *
5*aef5731fSOliver Tappe * The GNU LIBICONV Library is free software; you can redistribute it
6*aef5731fSOliver Tappe * and/or modify it under the terms of the GNU Library General Public
7*aef5731fSOliver Tappe * License as published by the Free Software Foundation; either version 2
8*aef5731fSOliver Tappe * of the License, or (at your option) any later version.
9*aef5731fSOliver Tappe *
10*aef5731fSOliver Tappe * The GNU LIBICONV Library is distributed in the hope that it will be
11*aef5731fSOliver Tappe * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12*aef5731fSOliver Tappe * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*aef5731fSOliver Tappe * Library General Public License for more details.
14*aef5731fSOliver Tappe *
15*aef5731fSOliver Tappe * You should have received a copy of the GNU Library General Public
16*aef5731fSOliver Tappe * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17*aef5731fSOliver Tappe * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
18*aef5731fSOliver Tappe * Fifth Floor, Boston, MA 02110-1301, USA.
19*aef5731fSOliver Tappe */
20*aef5731fSOliver Tappe
21*aef5731fSOliver Tappe /*
22*aef5731fSOliver Tappe * ISO-IR-165
23*aef5731fSOliver Tappe */
24*aef5731fSOliver Tappe
25*aef5731fSOliver Tappe /*
26*aef5731fSOliver Tappe * ISO-IR-165 is an extension of GB 2312, consisting of:
27*aef5731fSOliver Tappe * 1. GB 6345.1-86 corrections:
28*aef5731fSOliver Tappe * Two corrections to GB 2312, at 0x2367 and 0x6F71.
29*aef5731fSOliver Tappe * 2. GB 6345.1-86 additions:
30*aef5731fSOliver Tappe * - 6 new full-width pinyin characters in row 0x28.
31*aef5731fSOliver Tappe * - ISO646-CN in row 0x2A.
32*aef5731fSOliver Tappe * - 32 half-width pinyin characters in row 0x2B.
33*aef5731fSOliver Tappe * 3. GB 8565.2-88 additions:
34*aef5731fSOliver Tappe * - 50 characters in row 0x2D.
35*aef5731fSOliver Tappe * - 92 characters in row 0x2E.
36*aef5731fSOliver Tappe * - 93 characters in row 0x2F.
37*aef5731fSOliver Tappe * - 470 characters in rows 0x7A-0x7E.
38*aef5731fSOliver Tappe * 4. ISO-IR-165 additions:
39*aef5731fSOliver Tappe * - 22 characters in row 0x26.
40*aef5731fSOliver Tappe * - 94 characters in row 0x2C.
41*aef5731fSOliver Tappe * - 44 new characters in row 0x2D.
42*aef5731fSOliver Tappe * - 1 new character in row 0x2F.
43*aef5731fSOliver Tappe *
44*aef5731fSOliver Tappe * The conversion table was created from the following sources:
45*aef5731fSOliver Tappe * Ad 1. The 0x2367 correction is already integrated in the unicode.org
46*aef5731fSOliver Tappe * GB2312.TXT table. The 0x6F71 mapping is the same in the unicode.org
47*aef5731fSOliver Tappe * GB2312.TXT and UNIHAN.TXT table and in Koichi Yasuoka's Uni2GB table,
48*aef5731fSOliver Tappe * so we assume it's correct.
49*aef5731fSOliver Tappe * The unicode.org UNIHAN.TXT table about GB 8565 is not usable: it has
50*aef5731fSOliver Tappe * extraneous code points at rows 0x28, 0x2C, 0x2D. Note also that it does
51*aef5731fSOliver Tappe * not list the 69 non-hanzi in row 0x2F. Moreover, it has the characters
52*aef5731fSOliver Tappe * 0x2F7A-0x2F7D shifted down by one to 0x2F79-0x2F7C.
53*aef5731fSOliver Tappe * Therefore we take the GB8565 and ISO-IR-165 data from Koichi Yasuoka's
54*aef5731fSOliver Tappe * Uni2GB table.
55*aef5731fSOliver Tappe * Ad 1. Yasuoka maps 0x2367 to U+0261 (small script g) and 0x2840 to U+FF47
56*aef5731fSOliver Tappe * (full-width small normal g). While coherent with ISO-IR's 165.pdf,
57*aef5731fSOliver Tappe * this disagrees with Ken Lunde's book: He says that ISO-IR-165
58*aef5731fSOliver Tappe * includes the GB6345 correction, i.e. maps 0x2367 to U+FF47 or U+0067
59*aef5731fSOliver Tappe * and _not_ to U+0261 (small script g).
60*aef5731fSOliver Tappe * To overcome the confusion, we just map both 0x2367 and 0x2840 to
61*aef5731fSOliver Tappe * U+FF47.
62*aef5731fSOliver Tappe * Ad 2. Row 0x28: Add a mapping from 0x283F to U+01F9.
63*aef5731fSOliver Tappe * Row 0x2A: Mapping is well-known, also present in Koichi Yasuoka's
64*aef5731fSOliver Tappe * table.
65*aef5731fSOliver Tappe * Row 0x2B: Typed in by hand from appendix E in Ken Lunde's book.
66*aef5731fSOliver Tappe * When converting from Unicode to ISO-IR-165, prefer the half-width
67*aef5731fSOliver Tappe * range 0x2B{21..40} to the full-width range 0x28{21..40}.
68*aef5731fSOliver Tappe * Ad 3. Rows 0x2D, 0x2E: Both Koichi Yasuoka's Uni2GB table and the UNIHAN.TXT
69*aef5731fSOliver Tappe * data for GB 8565 agree here.
70*aef5731fSOliver Tappe * Row 0x2F: Taken from Koichi Yasuoka's Uni2GB table.
71*aef5731fSOliver Tappe * Rows 0x7A-0x7E: Koichi Yasuoka's Uni2GB table and the UNIHAN.TXT
72*aef5731fSOliver Tappe * data for GB 8565 agree here mostly. Differences:
73*aef5731fSOliver Tappe * 0x7C38 -> U+6F26 or U+527A ? We choose U+6F26.
74*aef5731fSOliver Tappe * 0x7C5A -> U+7A40 or U+6996 ? We choose U+6996.
75*aef5731fSOliver Tappe * Ad 4. Row 0x26: Mapping unknown.
76*aef5731fSOliver Tappe * Rows 0x2C, 0x2D: Both Koichi Yasuoka's Uni2GB table and the UNIHAN.TXT
77*aef5731fSOliver Tappe * data for GB 8565 (!) agree here.
78*aef5731fSOliver Tappe * Row 0x2F: Taken from Koichi Yasuoka's Uni2GB table.
79*aef5731fSOliver Tappe */
80*aef5731fSOliver Tappe
81*aef5731fSOliver Tappe #include "isoir165ext.h"
82*aef5731fSOliver Tappe
83*aef5731fSOliver Tappe static int
isoir165_mbtowc(conv_t conv,ucs4_t * pwc,const unsigned char * s,int n)84*aef5731fSOliver Tappe isoir165_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
85*aef5731fSOliver Tappe {
86*aef5731fSOliver Tappe int ret;
87*aef5731fSOliver Tappe
88*aef5731fSOliver Tappe /* Map full-width pinyin (row 0x28) like half-width pinyin (row 0x2B). */
89*aef5731fSOliver Tappe if (s[0] == 0x28) {
90*aef5731fSOliver Tappe if (n >= 2) {
91*aef5731fSOliver Tappe unsigned char c2 = s[1];
92*aef5731fSOliver Tappe if (c2 >= 0x21 && c2 <= 0x40) {
93*aef5731fSOliver Tappe unsigned char buf[2];
94*aef5731fSOliver Tappe buf[0] = 0x2b;
95*aef5731fSOliver Tappe buf[1] = c2;
96*aef5731fSOliver Tappe ret = isoir165ext_mbtowc(conv,pwc,buf,2);
97*aef5731fSOliver Tappe if (ret != RET_ILSEQ)
98*aef5731fSOliver Tappe return ret;
99*aef5731fSOliver Tappe }
100*aef5731fSOliver Tappe }
101*aef5731fSOliver Tappe }
102*aef5731fSOliver Tappe /* Try the GB2312 -> Unicode table. */
103*aef5731fSOliver Tappe ret = gb2312_mbtowc(conv,pwc,s,n);
104*aef5731fSOliver Tappe if (ret != RET_ILSEQ)
105*aef5731fSOliver Tappe return ret;
106*aef5731fSOliver Tappe /* Row 0x2A is GB_1988-80. */
107*aef5731fSOliver Tappe if (s[0] == 0x2a) {
108*aef5731fSOliver Tappe if (n >= 2) {
109*aef5731fSOliver Tappe unsigned char c2 = s[1];
110*aef5731fSOliver Tappe if (c2 >= 0x21 && c2 < 0x7f) {
111*aef5731fSOliver Tappe int ret = iso646_cn_mbtowc(conv,pwc,s+1,1);
112*aef5731fSOliver Tappe if (ret != 1) abort();
113*aef5731fSOliver Tappe return 2;
114*aef5731fSOliver Tappe }
115*aef5731fSOliver Tappe return RET_ILSEQ;
116*aef5731fSOliver Tappe }
117*aef5731fSOliver Tappe return RET_TOOFEW(0);
118*aef5731fSOliver Tappe }
119*aef5731fSOliver Tappe /* Try the ISO-IR-165 extensions -> Unicode table. */
120*aef5731fSOliver Tappe ret = isoir165ext_mbtowc(conv,pwc,s,n);
121*aef5731fSOliver Tappe return ret;
122*aef5731fSOliver Tappe }
123*aef5731fSOliver Tappe
124*aef5731fSOliver Tappe static int
isoir165_wctomb(conv_t conv,unsigned char * r,ucs4_t wc,int n)125*aef5731fSOliver Tappe isoir165_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
126*aef5731fSOliver Tappe {
127*aef5731fSOliver Tappe unsigned char buf[2];
128*aef5731fSOliver Tappe int ret;
129*aef5731fSOliver Tappe
130*aef5731fSOliver Tappe /* Try the Unicode -> GB2312 table. */
131*aef5731fSOliver Tappe ret = gb2312_wctomb(conv,buf,wc,2);
132*aef5731fSOliver Tappe if (ret != RET_ILUNI) {
133*aef5731fSOliver Tappe if (ret != 2) abort();
134*aef5731fSOliver Tappe if (!(buf[0] == 0x28 && buf[1] >= 0x21 && buf[1] <= 0x40)) {
135*aef5731fSOliver Tappe if (n >= 2) {
136*aef5731fSOliver Tappe r[0] = buf[0];
137*aef5731fSOliver Tappe r[1] = buf[1];
138*aef5731fSOliver Tappe return 2;
139*aef5731fSOliver Tappe }
140*aef5731fSOliver Tappe return RET_TOOSMALL;
141*aef5731fSOliver Tappe }
142*aef5731fSOliver Tappe }
143*aef5731fSOliver Tappe /* Row 0x2A is GB_1988-80. */
144*aef5731fSOliver Tappe ret = iso646_cn_wctomb(conv,buf,wc,1);
145*aef5731fSOliver Tappe if (ret != RET_ILUNI) {
146*aef5731fSOliver Tappe if (ret != 1) abort();
147*aef5731fSOliver Tappe if (buf[0] >= 0x21 && buf[0] < 0x7f) {
148*aef5731fSOliver Tappe if (n >= 2) {
149*aef5731fSOliver Tappe r[0] = 0x2a;
150*aef5731fSOliver Tappe r[1] = buf[0];
151*aef5731fSOliver Tappe return 2;
152*aef5731fSOliver Tappe }
153*aef5731fSOliver Tappe return RET_TOOSMALL;
154*aef5731fSOliver Tappe }
155*aef5731fSOliver Tappe }
156*aef5731fSOliver Tappe /* Try the Unicode -> ISO-IR-165 extensions table. */
157*aef5731fSOliver Tappe ret = isoir165ext_wctomb(conv,r,wc,n);
158*aef5731fSOliver Tappe return ret;
159*aef5731fSOliver Tappe }
160