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