Switch to autoconf 2.72.
[libiconv.git] / lib / gb18030_2022.h
blob4d70a927d40c101765678f6055995bdd3422a3db
1 /*
2 * Copyright (C) 1999-2001, 2005, 2012, 2016, 2023 Free Software Foundation, Inc.
3 * This file is part of the GNU LIBICONV Library.
5 * The GNU LIBICONV Library is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17 * If not, see <https://www.gnu.org/licenses/>.
21 * GB18030:2022
25 * GB18030, in the version from 2022, is like the version from 2005,
26 * except that a few mappings to the Unicode PUA have been replaced with
27 * mappings to assigned Unicode characters.
30 static int
31 gb18030_2022_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
33 int ret;
35 /* Code set 0 (ASCII) */
36 if (*s < 0x80)
37 return ascii_mbtowc(conv,pwc,s,n);
39 /* Code set 1 (GBK extended) */
40 ret = gbk_mbtowc(conv,pwc,s,n);
41 if (ret != RET_ILSEQ)
42 return ret;
44 ret = gb18030_2022_ext_mbtowc(conv,pwc,s,n);
45 if (ret != RET_ILSEQ)
46 return ret;
48 /* Code set 2 (remainder of Unicode U+0000..U+FFFF), including
49 User-defined characters, two-byte part of range U+E766..U+E864 */
50 ret = gb18030_2022_uni_mbtowc(conv,pwc,s,n);
51 if (ret != RET_ILSEQ)
52 return ret;
53 /* User-defined characters range U+E000..U+E765 */
55 unsigned char c1 = s[0];
56 if ((c1 >= 0xaa && c1 <= 0xaf) || (c1 >= 0xf8 && c1 <= 0xfe)) {
57 if (n >= 2) {
58 unsigned char c2 = s[1];
59 if (c2 >= 0xa1 && c2 <= 0xfe) {
60 *pwc = 0xe000 + 94 * (c1 >= 0xf8 ? c1 - 0xf2 : c1 - 0xaa) + (c2 - 0xa1);
61 return 2;
63 } else
64 return RET_TOOFEW(0);
65 } else if (c1 >= 0xa1 && c1 <= 0xa7) {
66 if (n >= 2) {
67 unsigned char c2 = s[1];
68 if (c2 >= 0x40 && c2 <= 0xa1 && c2 != 0x7f) {
69 *pwc = 0xe4c6 + 96 * (c1 - 0xa1) + c2 - (c2 >= 0x80 ? 0x41 : 0x40);
70 return 2;
72 } else
73 return RET_TOOFEW(0);
77 /* Code set 3 (Unicode U+10000..U+10FFFF) */
79 unsigned char c1 = s[0];
80 if (c1 >= 0x90 && c1 <= 0xe3) {
81 if (n >= 2) {
82 unsigned char c2 = s[1];
83 if (c2 >= 0x30 && c2 <= 0x39) {
84 if (n >= 3) {
85 unsigned char c3 = s[2];
86 if (c3 >= 0x81 && c3 <= 0xfe) {
87 if (n >= 4) {
88 unsigned char c4 = s[3];
89 if (c4 >= 0x30 && c4 <= 0x39) {
90 unsigned int i = (((c1 - 0x90) * 10 + (c2 - 0x30)) * 126 + (c3 - 0x81)) * 10 + (c4 - 0x30);
91 if (i >= 0 && i < 0x100000) {
92 *pwc = (ucs4_t) (0x10000 + i);
93 return 4;
96 return RET_ILSEQ;
98 return RET_TOOFEW(0);
100 return RET_ILSEQ;
102 return RET_TOOFEW(0);
104 return RET_ILSEQ;
106 return RET_TOOFEW(0);
108 return RET_ILSEQ;
112 static const unsigned short gb18030_2022_pua2charset[23*3] = {
113 /* Unicode range GB18030 range */
114 0xe766, 0xe76b, 0xa2ab, /*.. 0xa2b0, */
115 0xe76d, 0xe76d, 0xa2e4,
116 0xe76e, 0xe76f, 0xa2ef, /*.. 0xa2f0, */
117 0xe770, 0xe771, 0xa2fd, /*.. 0xa2fe, */
118 0xe772, 0xe77c, 0xa4f4, /*.. 0xa4fe, */
119 0xe77d, 0xe784, 0xa5f7, /*.. 0xa5fe, */
120 0xe785, 0xe78c, 0xa6b9, /*.. 0xa6c0, */
121 0xe797, 0xe79f, 0xa6f6, /*.. 0xa6fe, */
122 0xe7a0, 0xe7ae, 0xa7c2, /*.. 0xa7d0, */
123 0xe7af, 0xe7bb, 0xa7f2, /*.. 0xa7fe, */
124 0xe7bc, 0xe7c6, 0xa896, /*.. 0xa8a0, */
125 0xe7c9, 0xe7cc, 0xa8c1, /*.. 0xa8c4, */
126 0xe7cd, 0xe7e1, 0xa8ea, /*.. 0xa8fe, */
127 0xe7e2, 0xe7e2, 0xa958,
128 0xe7e3, 0xe7e3, 0xa95b,
129 0xe7e4, 0xe7e6, 0xa95d, /*.. 0xa95f, */
130 0xe7f4, 0xe800, 0xa997, /*.. 0xa9a3, */
131 0xe801, 0xe80f, 0xa9f0, /*.. 0xa9fe, */
132 0xe810, 0xe814, 0xd7fa, /*.. 0xd7fe, */
133 0xe816, 0xe818, 0xfe51, /*.. 0xfe53, */
134 0xe831, 0xe831, 0xfe6c,
135 0xe83b, 0xe83b, 0xfe76,
136 0xe855, 0xe855, 0xfe91,
139 static int
140 gb18030_2022_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
142 int ret;
144 /* Code set 0 (ASCII) */
145 ret = ascii_wctomb(conv,r,wc,n);
146 if (ret != RET_ILUNI)
147 return ret;
149 /* Code set 1 (GBK extended) */
150 ret = gbk_wctomb(conv,r,wc,n);
151 if (ret != RET_ILUNI)
152 return ret;
154 ret = gb18030ext_wctomb(conv,r,wc,n);
155 if (ret != RET_ILUNI)
156 return ret;
158 /* Code set 2 (remainder of Unicode U+0000..U+FFFF) */
159 if (wc >= 0xe000 && wc <= 0xe864) {
160 if (n >= 2) {
161 if (wc < 0xe766) {
162 /* User-defined characters range U+E000..U+E765 */
163 if (wc < 0xe4c6) {
164 unsigned int i = wc - 0xe000;
165 r[1] = (i % 94) + 0xa1; i = i / 94;
166 r[0] = (i < 6 ? i + 0xaa : i + 0xf2);
167 return 2;
168 } else {
169 unsigned int i = wc - 0xe4c6;
170 r[0] = (i / 96) + 0xa1; i = i % 96;
171 r[1] = i + (i >= 0x3f ? 0x41 : 0x40);
172 return 2;
174 } else {
175 /* User-defined characters, two-byte part of range U+E766..U+E864 */
176 unsigned int k1 = 0;
177 unsigned int k2 = 23;
178 /* Invariant: We know that if wc occurs in Unicode interval in
179 gb18030_2022_pua2charset, it does so at a k with k1 <= k < k2. */
180 while (k1 < k2) {
181 unsigned int k = (k1 + k2) / 2;
182 if (wc < gb18030_2022_pua2charset[k*3+0])
183 k2 = k;
184 else if (wc > gb18030_2022_pua2charset[k*3+1])
185 k1 = k + 1;
186 else {
187 unsigned short c =
188 gb18030_2022_pua2charset[k*3+2] + (wc - gb18030_2022_pua2charset[k*3+0]);
189 r[0] = (c >> 8);
190 r[1] = (c & 0xff);
191 return 2;
195 } else
196 return RET_TOOSMALL;
198 ret = gb18030_2022_uni_wctomb(conv,r,wc,n);
199 if (ret != RET_ILUNI)
200 return ret;
202 /* Code set 3 (Unicode U+10000..U+10FFFF) */
203 if (n >= 4) {
204 if (wc >= 0x10000 && wc < 0x110000) {
205 unsigned int i = wc - 0x10000;
206 r[3] = (i % 10) + 0x30; i = i / 10;
207 r[2] = (i % 126) + 0x81; i = i / 126;
208 r[1] = (i % 10) + 0x30; i = i / 10;
209 r[0] = i + 0x90;
210 return 4;
212 return RET_ILUNI;
214 return RET_TOOSMALL;