]> git.lyx.org Git - lyx.git/blob - 3rdparty/libiconv/1.15/lib/gbk.h
Implement auto-nesting.
[lyx.git] / 3rdparty / libiconv / 1.15 / lib / gbk.h
1 /*
2  * Copyright (C) 1999-2001, 2005, 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 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * GBK
22  */
23
24 /*
25  * GBK, as described in Ken Lunde's book, is an extension of GB 2312-1980
26  * (shifted by adding 0x8080 to the range 0xA1A1..0xFEFE, as used in EUC-CN).
27  * It adds the following ranges:
28  *
29  * (part of GBK/1)  0xA2A1-0xA2AA  Small Roman numerals
30  * GBK/3   0x{81-A0}{40-7E,80-FE}  6080 new characters, all in Unicode
31  * GBK/4   0x{AA-FE}{40-7E,80-A0}  8160 new characters, 8080 in Unicode
32  * GBK/5   0x{A8-A9}{40-7E,80-A0}  166 new characters, 153 in Unicode
33  *
34  * Furthermore, all four tables I have looked at
35  *   - the CP936 table by Microsoft, found on ftp.unicode.org in 1999,
36  *   - the GBK table by Sun, investigated on a Solaris 2.7 machine,
37  *   - the GBK tables by CWEX, found in the Big5+ package,
38  *   - the GB18030 standard (second printing),
39  * agree in the following extensions. (Ken Lunde must have overlooked these
40  * differences between GB2312 and GBK. Also, the CWEX tables have additional
41  * differences.)
42  *
43  * 1. Some characters in the GB2312 range are defined differently:
44  *
45  *     code    GB2312                         GBK
46  *    0xA1A4   0x30FB # KATAKANA MIDDLE DOT   0x00B7 # MIDDLE DOT
47  *    0xA1AA   0x2015 # HORIZONTAL BAR        0x2014 # EM DASH
48  *
49  * 2. 19 characters added in the range 0xA6E0-0xA6F5.
50  *
51  * 3. 4 characters added in the range 0xA8BB-0xA8C0.
52  *
53  * CP936 as of 1999 was identical to GBK. However, since 1999, Microsoft has
54  * added new mappings to CP936...
55  */
56
57 #include "gbkext1.h"
58 #include "gbkext2.h"
59 #include "gbkext_inv.h"
60 #include "cp936ext.h"
61
62 static int
63 gbk_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
64 {
65   unsigned char c = *s;
66
67   if (c >= 0x81 && c < 0xff) {
68     if (n < 2)
69       return RET_TOOFEW(0);
70     if (c >= 0xa1 && c <= 0xf7) {
71       unsigned char c2 = s[1];
72       if (c == 0xa1) {
73         if (c2 == 0xa4) {
74           *pwc = 0x00b7;
75           return 2;
76         }
77         if (c2 == 0xaa) {
78           *pwc = 0x2014;
79           return 2;
80         }
81       }
82       if (c2 >= 0xa1 && c2 < 0xff) {
83         unsigned char buf[2];
84         int ret;
85         buf[0] = c-0x80; buf[1] = c2-0x80;
86         ret = gb2312_mbtowc(conv,pwc,buf,2);
87         if (ret != RET_ILSEQ)
88           return ret;
89         buf[0] = c; buf[1] = c2;
90         ret = cp936ext_mbtowc(conv,pwc,buf,2);
91         if (ret != RET_ILSEQ)
92           return ret;
93       }
94     }
95     if (c >= 0x81 && c <= 0xa0)
96       return gbkext1_mbtowc(conv,pwc,s,2);
97     if (c >= 0xa8 && c <= 0xfe)
98       return gbkext2_mbtowc(conv,pwc,s,2);
99     if (c == 0xa2) {
100       unsigned char c2 = s[1];
101       if (c2 >= 0xa1 && c2 <= 0xaa) {
102         *pwc = 0x2170+(c2-0xa1);
103         return 2;
104       }
105     }
106   }
107   return RET_ILSEQ;
108 }
109
110 static int
111 gbk_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
112 {
113   unsigned char buf[2];
114   int ret;
115
116   if (wc != 0x30fb && wc != 0x2015) {
117     ret = gb2312_wctomb(conv,buf,wc,2);
118     if (ret != RET_ILUNI) {
119       if (ret != 2) abort();
120       if (n < 2)
121         return RET_TOOSMALL;
122       r[0] = buf[0]+0x80;
123       r[1] = buf[1]+0x80;
124       return 2;
125     }
126   }
127   ret = gbkext_inv_wctomb(conv,buf,wc,2);
128   if (ret != RET_ILUNI) {
129     if (ret != 2) abort();
130     if (n < 2)
131       return RET_TOOSMALL;
132     r[0] = buf[0];
133     r[1] = buf[1];
134     return 2;
135   }
136   if (wc >= 0x2170 && wc <= 0x2179) {
137     if (n < 2)
138       return RET_TOOSMALL;
139     r[0] = 0xa2;
140     r[1] = 0xa1 + (wc-0x2170);
141     return 2;
142   }
143   ret = cp936ext_wctomb(conv,buf,wc,2);
144   if (ret != RET_ILUNI) {
145     if (ret != 2) abort();
146     if (n < 2)
147       return RET_TOOSMALL;
148     r[0] = buf[0];
149     r[1] = buf[1];
150     return 2;
151   }
152   if (wc == 0x00b7) {
153     if (n < 2)
154       return RET_TOOSMALL;
155     r[0] = 0xa1;
156     r[1] = 0xa4;
157     return 2;
158   }
159   if (wc == 0x2014) {
160     if (n < 2)
161       return RET_TOOSMALL;
162     r[0] = 0xa1;
163     r[1] = 0xaa;
164     return 2;
165   }
166
167   return RET_ILUNI;
168 }