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/>.
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.
31 gb18030_2022_mbtowc (conv_t conv
, ucs4_t
*pwc
, const unsigned char *s
, size_t n
)
35 /* Code set 0 (ASCII) */
37 return ascii_mbtowc(conv
,pwc
,s
,n
);
39 /* Code set 1 (GBK extended) */
40 ret
= gbk_mbtowc(conv
,pwc
,s
,n
);
44 ret
= gb18030_2022_ext_mbtowc(conv
,pwc
,s
,n
);
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
);
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)) {
58 unsigned char c2
= s
[1];
59 if (c2
>= 0xa1 && c2
<= 0xfe) {
60 *pwc
= 0xe000 + 94 * (c1
>= 0xf8 ? c1
- 0xf2 : c1
- 0xaa) + (c2
- 0xa1);
65 } else if (c1
>= 0xa1 && c1
<= 0xa7) {
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);
77 /* Code set 3 (Unicode U+10000..U+10FFFF) */
79 unsigned char c1
= s
[0];
80 if (c1
>= 0x90 && c1
<= 0xe3) {
82 unsigned char c2
= s
[1];
83 if (c2
>= 0x30 && c2
<= 0x39) {
85 unsigned char c3
= s
[2];
86 if (c3
>= 0x81 && c3
<= 0xfe) {
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
);
102 return RET_TOOFEW(0);
106 return RET_TOOFEW(0);
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,
140 gb18030_2022_wctomb (conv_t conv
, unsigned char *r
, ucs4_t wc
, size_t n
)
144 /* Code set 0 (ASCII) */
145 ret
= ascii_wctomb(conv
,r
,wc
,n
);
146 if (ret
!= RET_ILUNI
)
149 /* Code set 1 (GBK extended) */
150 ret
= gbk_wctomb(conv
,r
,wc
,n
);
151 if (ret
!= RET_ILUNI
)
154 ret
= gb18030ext_wctomb(conv
,r
,wc
,n
);
155 if (ret
!= RET_ILUNI
)
158 /* Code set 2 (remainder of Unicode U+0000..U+FFFF) */
159 if (wc
>= 0xe000 && wc
<= 0xe864) {
162 /* User-defined characters range U+E000..U+E765 */
164 unsigned int i
= wc
- 0xe000;
165 r
[1] = (i
% 94) + 0xa1; i
= i
/ 94;
166 r
[0] = (i
< 6 ? i
+ 0xaa : i
+ 0xf2);
169 unsigned int i
= wc
- 0xe4c6;
170 r
[0] = (i
/ 96) + 0xa1; i
= i
% 96;
171 r
[1] = i
+ (i
>= 0x3f ? 0x41 : 0x40);
175 /* User-defined characters, two-byte part of range U+E766..U+E864 */
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. */
181 unsigned int k
= (k1
+ k2
) / 2;
182 if (wc
< gb18030_2022_pua2charset
[k
*3+0])
184 else if (wc
> gb18030_2022_pua2charset
[k
*3+1])
188 gb18030_2022_pua2charset
[k
*3+2] + (wc
- gb18030_2022_pua2charset
[k
*3+0]);
198 ret
= gb18030_2022_uni_wctomb(conv
,r
,wc
,n
);
199 if (ret
!= RET_ILUNI
)
202 /* Code set 3 (Unicode U+10000..U+10FFFF) */
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;