]>
Commit | Line | Data |
---|---|---|
dbd10df7 | 1 | /* Conversion from and to IBM935 |
2b778ceb | 2 | Copyright (C) 2000-2021 Free Software Foundation, Inc. |
dbd10df7 | 3 | This file is part of the GNU C Library. |
dbd10df7 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
dbd10df7 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
dbd10df7 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
dbd10df7 | 18 | |
4de3b51e CD |
19 | /* IBM935 is designed for the representation of Simplified Chinese |
20 | using a stateful EBCDIC encoding scheme. It is also known as | |
21 | CCSID 935 or CP935. See: | |
22 | https://www-01.ibm.com/software/globalization/ccsid/ccsid935.html */ | |
23 | ||
dbd10df7 UD |
24 | #include <dlfcn.h> |
25 | #include <stdint.h> | |
26 | #include <wchar.h> | |
27 | #include <byteswap.h> | |
28 | #include "ibm935.h" | |
29 | ||
30 | /* The shift sequences for this charset (it does not use ESC). */ | |
31 | #define SI 0x0F /* Shift In, host code to turn DBCS off. */ | |
32 | #define SO 0x0E /* Shift Out, host code to turn DBCS on. */ | |
33 | ||
34 | /* Definitions used in the body of the `gconv' function. */ | |
35 | #define CHARSET_NAME "IBM935//" | |
36 | #define FROM_LOOP from_ibm935 | |
37 | #define TO_LOOP to_ibm935 | |
13e402e7 | 38 | #define ONE_DIRECTION 0 |
faaa6f62 UD |
39 | #define FROM_LOOP_MIN_NEEDED_FROM 1 |
40 | #define FROM_LOOP_MAX_NEEDED_FROM 2 | |
41 | #define FROM_LOOP_MIN_NEEDED_TO 4 | |
42 | #define FROM_LOOP_MAX_NEEDED_TO 4 | |
43 | #define TO_LOOP_MIN_NEEDED_FROM 4 | |
44 | #define TO_LOOP_MAX_NEEDED_FROM 4 | |
45 | #define TO_LOOP_MIN_NEEDED_TO 1 | |
46 | #define TO_LOOP_MAX_NEEDED_TO 3 | |
f1813b56 UD |
47 | #define PREPARE_LOOP \ |
48 | int save_curcs; \ | |
49 | int *curcsp = &data->__statep->__count; | |
50 | #define EXTRA_LOOP_ARGS , curcsp | |
dbd10df7 UD |
51 | |
52 | /* Definitions of initialization and destructor function. */ | |
53 | #define DEFINE_INIT 1 | |
54 | #define DEFINE_FINI 1 | |
55 | ||
f1813b56 UD |
56 | |
57 | /* Since this is a stateful encoding we have to provide code which resets | |
58 | the output state to the initial state. This has to be done during the | |
59 | flushing. */ | |
60 | #define EMIT_SHIFT_TO_INIT \ | |
61 | if ((data->__statep->__count & ~7) != sb) \ | |
62 | { \ | |
63 | if (FROM_DIRECTION) \ | |
64 | data->__statep->__count &= 7; \ | |
65 | else \ | |
66 | { \ | |
f1813b56 UD |
67 | /* We are not in the initial state. To switch back we have \ |
68 | to emit `SI'. */ \ | |
a1ffb40e | 69 | if (__glibc_unlikely (outbuf >= outend)) \ |
f1813b56 UD |
70 | /* We don't have enough room in the output buffer. */ \ |
71 | status = __GCONV_FULL_OUTPUT; \ | |
72 | else \ | |
73 | { \ | |
74 | /* Write out the shift sequence. */ \ | |
75 | *outbuf++ = SI; \ | |
f1813b56 UD |
76 | data->__statep->__count &= 7; \ |
77 | } \ | |
78 | } \ | |
79 | } | |
80 | ||
81 | ||
82 | /* Since we might have to reset input pointer we must be able to save | |
83 | and retore the state. */ | |
84 | #define SAVE_RESET_STATE(Save) \ | |
85 | if (Save) \ | |
86 | save_curcs = *curcsp; \ | |
87 | else \ | |
88 | *curcsp = save_curcs | |
89 | ||
dbd10df7 UD |
90 | |
91 | /* Current codeset type. */ | |
92 | enum | |
93 | { | |
f1813b56 UD |
94 | sb = 0, |
95 | db = 64 | |
dbd10df7 UD |
96 | }; |
97 | ||
98 | /* First, define the conversion function from IBM-935 to UCS4. */ | |
faaa6f62 UD |
99 | #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM |
100 | #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM | |
101 | #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO | |
102 | #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO | |
dbd10df7 UD |
103 | #define LOOPFCT FROM_LOOP |
104 | #define BODY \ | |
105 | { \ | |
106 | uint32_t ch = *inptr; \ | |
107 | uint32_t res; \ | |
dbd10df7 UD |
108 | \ |
109 | if (__builtin_expect(ch, 0) == SO) \ | |
110 | { \ | |
692de4b3 | 111 | /* Shift OUT, change to DBCS converter (redundant escape okay). */ \ |
dbd10df7 UD |
112 | curcs = db; \ |
113 | ++inptr; \ | |
f1813b56 | 114 | continue; \ |
dbd10df7 UD |
115 | } \ |
116 | else if (__builtin_expect (ch, 0) == SI) \ | |
117 | { \ | |
692de4b3 | 118 | /* Shift IN, change to SBCS converter (redundant escape okay). */ \ |
dbd10df7 UD |
119 | curcs = sb; \ |
120 | ++inptr; \ | |
f1813b56 | 121 | continue; \ |
dbd10df7 UD |
122 | } \ |
123 | \ | |
f1813b56 | 124 | if (curcs == sb) \ |
dbd10df7 UD |
125 | { \ |
126 | /* Use the IBM935 table for single byte. */ \ | |
f1813b56 UD |
127 | res = __ibm935sb_to_ucs4[ch]; \ |
128 | if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \ | |
dbd10df7 UD |
129 | { \ |
130 | /* This is an illegal character. */ \ | |
e438a468 | 131 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
dbd10df7 UD |
132 | } \ |
133 | else \ | |
134 | { \ | |
135 | put32 (outptr, res); \ | |
136 | outptr += 4; \ | |
dbd10df7 | 137 | } \ |
f1813b56 | 138 | ++inptr; \ |
dbd10df7 | 139 | } \ |
f1813b56 | 140 | else \ |
dbd10df7 | 141 | { \ |
f1813b56 UD |
142 | const struct gap *rp2 = __ibm935db_to_ucs4_idx; \ |
143 | \ | |
144 | assert (curcs == db); \ | |
145 | \ | |
dbd10df7 | 146 | /* Use the IBM935 table for double byte. */ \ |
a1ffb40e | 147 | if (__glibc_unlikely (inptr + 1 >= inend)) \ |
dbd10df7 UD |
148 | { \ |
149 | /* The second character is not available. \ | |
150 | Store the intermediate result. */ \ | |
151 | result = __GCONV_INCOMPLETE_INPUT; \ | |
152 | break; \ | |
153 | } \ | |
154 | \ | |
155 | ch = (ch * 0x100) + inptr[1]; \ | |
156 | while (ch > rp2->end) \ | |
157 | ++rp2; \ | |
158 | \ | |
41488498 | 159 | if (__builtin_expect (rp2->start == 0xffff, 0) \ |
dbd10df7 UD |
160 | || __builtin_expect (ch < rp2->start, 0) \ |
161 | || (res = __ibm935db_to_ucs4[ch + rp2->idx], \ | |
162 | __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \ | |
163 | { \ | |
164 | /* This is an illegal character. */ \ | |
e438a468 | 165 | STANDARD_FROM_LOOP_ERR_HANDLER (2); \ |
dbd10df7 UD |
166 | } \ |
167 | else \ | |
168 | { \ | |
169 | put32 (outptr, res); \ | |
170 | outptr += 4; \ | |
dbd10df7 | 171 | } \ |
f1813b56 | 172 | inptr += 2; \ |
dbd10df7 UD |
173 | } \ |
174 | } | |
175 | #define LOOP_NEED_FLAGS | |
f1813b56 UD |
176 | #define EXTRA_LOOP_DECLS , int *curcsp |
177 | #define INIT_PARAMS int curcs = *curcsp & ~7 | |
178 | #define UPDATE_PARAMS *curcsp = curcs | |
dbd10df7 UD |
179 | #include <iconv/loop.c> |
180 | ||
181 | /* Next, define the other direction. */ | |
faaa6f62 UD |
182 | #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM |
183 | #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM | |
184 | #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO | |
185 | #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO | |
dbd10df7 UD |
186 | #define LOOPFCT TO_LOOP |
187 | #define BODY \ | |
188 | { \ | |
189 | uint32_t ch = get32 (inptr); \ | |
190 | const struct gap *rp1 = __ucs4_to_ibm935sb_idx; \ | |
191 | const struct gap *rp2 = __ucs4_to_ibm935db_idx; \ | |
dbd10df7 | 192 | \ |
a1ffb40e | 193 | if (__glibc_unlikely (ch >= 0xffff)) \ |
dbd10df7 | 194 | { \ |
601d2942 UD |
195 | UNICODE_TAG_HANDLER (ch, 4); \ |
196 | \ | |
e438a468 | 197 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
dbd10df7 | 198 | } \ |
f1813b56 UD |
199 | \ |
200 | while (ch > rp1->end) \ | |
201 | ++rp1; \ | |
dbd10df7 UD |
202 | \ |
203 | /* Use the UCS4 table for single byte. */ \ | |
b9deb8ce | 204 | unsigned char sbconv; \ |
f1813b56 | 205 | if (__builtin_expect (ch < rp1->start, 0) \ |
b9deb8ce FW |
206 | || (sbconv = __ucs4_to_ibm935sb[ch + rp1->idx], \ |
207 | __builtin_expect (sbconv, L'\1') == L'\0' && ch != '\0')) \ | |
dbd10df7 UD |
208 | { \ |
209 | /* Use the UCS4 table for double byte. */ \ | |
210 | while (ch > rp2->end) \ | |
211 | ++rp2; \ | |
212 | \ | |
b9deb8ce | 213 | const char *cp; \ |
f1813b56 | 214 | if (__builtin_expect (ch < rp2->start, 0) \ |
dbd10df7 UD |
215 | || (cp = __ucs4_to_ibm935db[ch + rp2->idx], \ |
216 | __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0')) \ | |
217 | { \ | |
218 | /* This is an illegal character. */ \ | |
e438a468 | 219 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
dbd10df7 UD |
220 | } \ |
221 | else \ | |
222 | { \ | |
f1813b56 | 223 | if (curcs == sb) \ |
dbd10df7 | 224 | { \ |
a1ffb40e | 225 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
226 | { \ |
227 | result = __GCONV_FULL_OUTPUT; \ | |
228 | break; \ | |
229 | } \ | |
230 | *outptr++ = SO; \ | |
231 | curcs = db; \ | |
232 | } \ | |
233 | \ | |
a1ffb40e | 234 | if (__glibc_unlikely (outptr + 2 > outend)) \ |
dbd10df7 UD |
235 | { \ |
236 | result = __GCONV_FULL_OUTPUT; \ | |
237 | break; \ | |
238 | } \ | |
239 | *outptr++ = cp[0]; \ | |
240 | *outptr++ = cp[1]; \ | |
241 | } \ | |
242 | } \ | |
243 | else \ | |
244 | { \ | |
245 | if (curcs == db) \ | |
246 | { \ | |
a1ffb40e | 247 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
248 | { \ |
249 | result = __GCONV_FULL_OUTPUT; \ | |
250 | break; \ | |
251 | } \ | |
252 | *outptr++ = SI; \ | |
42d6443f | 253 | curcs = sb; \ |
dbd10df7 UD |
254 | } \ |
255 | \ | |
a1ffb40e | 256 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
257 | { \ |
258 | result = __GCONV_FULL_OUTPUT; \ | |
259 | break; \ | |
260 | } \ | |
b9deb8ce | 261 | *outptr++ = sbconv; \ |
dbd10df7 UD |
262 | } \ |
263 | \ | |
264 | /* Now that we wrote the output increment the input pointer. */ \ | |
265 | inptr += 4; \ | |
266 | } | |
267 | #define LOOP_NEED_FLAGS | |
f1813b56 UD |
268 | #define EXTRA_LOOP_DECLS , int *curcsp |
269 | #define INIT_PARAMS int curcs = *curcsp & ~7 | |
4b1b449d | 270 | #define REINIT_PARAMS curcs = *curcsp & ~7 |
f1813b56 | 271 | #define UPDATE_PARAMS *curcsp = curcs |
dbd10df7 UD |
272 | #include <iconv/loop.c> |
273 | ||
274 | /* Now define the toplevel functions. */ | |
275 | #include <iconv/skeleton.c> |