]>
Commit | Line | Data |
---|---|---|
dbd10df7 | 1 | /* Conversion from and to IBM930. |
04277e02 | 2 | Copyright (C) 2000-2019 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 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
dbd10df7 | 19 | |
4de3b51e CD |
20 | /* IBM930 is designed for the representation of Japanese Katakana/Kanji |
21 | using a stateful EBCDIC encoding scheme. It is also known as | |
22 | CCSID 930 or CP930. See: | |
23 | https://www-01.ibm.com/software/globalization/ccsid/ccsid930.html */ | |
24 | ||
dbd10df7 UD |
25 | #include <dlfcn.h> |
26 | #include <stdint.h> | |
27 | #include <wchar.h> | |
28 | #include <byteswap.h> | |
29 | #include "ibm930.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 "IBM930//" | |
37 | #define FROM_LOOP from_ibm930 | |
38 | #define TO_LOOP to_ibm930 | |
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 | ||
f1813b56 | 99 | |
dbd10df7 | 100 | /* First, define the conversion function from IBM-930 to UCS4. */ |
faaa6f62 UD |
101 | #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM |
102 | #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM | |
103 | #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO | |
104 | #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO | |
dbd10df7 UD |
105 | #define LOOPFCT FROM_LOOP |
106 | #define BODY \ | |
107 | { \ | |
108 | uint32_t ch = *inptr; \ | |
109 | uint32_t res; \ | |
dbd10df7 UD |
110 | \ |
111 | if (__builtin_expect (ch, 0) == SO) \ | |
112 | { \ | |
692de4b3 | 113 | /* Shift OUT, change to DBCS converter (redundant escape okay). */ \ |
dbd10df7 UD |
114 | curcs = db; \ |
115 | ++inptr; \ | |
f1813b56 | 116 | continue; \ |
dbd10df7 UD |
117 | } \ |
118 | else if (__builtin_expect (ch, 0) == SI) \ | |
119 | { \ | |
692de4b3 | 120 | /* Shift IN, change to SBCS converter (redundant escape okay). */ \ |
dbd10df7 UD |
121 | curcs = sb; \ |
122 | ++inptr; \ | |
f1813b56 | 123 | continue; \ |
dbd10df7 UD |
124 | } \ |
125 | \ | |
f1813b56 | 126 | if (curcs == sb) \ |
dbd10df7 UD |
127 | { \ |
128 | /* Use the IBM930 table for single byte. */ \ | |
f1813b56 UD |
129 | res = __ibm930sb_to_ucs4[ch]; \ |
130 | if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \ | |
dbd10df7 UD |
131 | { \ |
132 | /* This is an illegal character. */ \ | |
e438a468 | 133 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
dbd10df7 UD |
134 | } \ |
135 | else \ | |
136 | { \ | |
dbd10df7 UD |
137 | put32 (outptr, res); \ |
138 | outptr += 4; \ | |
dbd10df7 | 139 | } \ |
f1813b56 | 140 | ++inptr; \ |
dbd10df7 | 141 | } \ |
f1813b56 | 142 | else \ |
dbd10df7 UD |
143 | { \ |
144 | /* Use the IBM930 table for double byte. */ \ | |
f1813b56 UD |
145 | const struct gap *rp2 = __ibm930db_to_ucs4_idx; \ |
146 | \ | |
147 | assert (curcs == db); \ | |
148 | \ | |
a1ffb40e | 149 | if (__glibc_unlikely (inptr + 1 >= inend)) \ |
dbd10df7 UD |
150 | { \ |
151 | /* The second character is not available. Store the \ | |
152 | intermediate result. */ \ | |
153 | result = __GCONV_INCOMPLETE_INPUT; \ | |
154 | break; \ | |
155 | } \ | |
156 | \ | |
157 | ch = (ch * 0x100) + inptr[1]; \ | |
158 | while (ch > rp2->end) \ | |
159 | ++rp2; \ | |
160 | \ | |
6e230d11 SP |
161 | if (__builtin_expect (rp2->start == 0xffff, 0) \ |
162 | || __builtin_expect (ch < rp2->start, 0) \ | |
dbd10df7 UD |
163 | || (res = __ibm930db_to_ucs4[ch + rp2->idx], \ |
164 | __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \ | |
165 | { \ | |
166 | /* This is an illegal character. */ \ | |
e438a468 | 167 | STANDARD_FROM_LOOP_ERR_HANDLER (2); \ |
dbd10df7 UD |
168 | } \ |
169 | else \ | |
170 | { \ | |
171 | put32 (outptr, res); \ | |
172 | outptr += 4; \ | |
dbd10df7 | 173 | } \ |
f1813b56 | 174 | inptr += 2; \ |
dbd10df7 UD |
175 | } \ |
176 | } | |
177 | #define LOOP_NEED_FLAGS | |
f1813b56 UD |
178 | #define EXTRA_LOOP_DECLS , int *curcsp |
179 | #define INIT_PARAMS int curcs = *curcsp & ~7 | |
180 | #define UPDATE_PARAMS *curcsp = curcs | |
dbd10df7 UD |
181 | #include <iconv/loop.c> |
182 | ||
183 | /* Next, define the other direction. */ | |
faaa6f62 UD |
184 | #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM |
185 | #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM | |
186 | #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO | |
187 | #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO | |
dbd10df7 UD |
188 | #define LOOPFCT TO_LOOP |
189 | #define BODY \ | |
190 | { \ | |
191 | uint32_t ch = get32 (inptr); \ | |
192 | const struct gap *rp1 = __ucs4_to_ibm930sb_idx; \ | |
193 | const struct gap *rp2 = __ucs4_to_ibm930db_idx; \ | |
dbd10df7 | 194 | \ |
a1ffb40e | 195 | if (__glibc_unlikely (ch >= 0xffff)) \ |
dbd10df7 | 196 | { \ |
601d2942 UD |
197 | UNICODE_TAG_HANDLER (ch, 4); \ |
198 | \ | |
e438a468 | 199 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
dbd10df7 | 200 | } \ |
f1813b56 UD |
201 | \ |
202 | while (ch > rp1->end) \ | |
203 | ++rp1; \ | |
dbd10df7 UD |
204 | \ |
205 | /* Use the UCS4 table for single byte. */ \ | |
b9deb8ce | 206 | unsigned char sbconv; \ |
faaa6f62 | 207 | if (__builtin_expect (ch < rp1->start, 0) \ |
b9deb8ce FW |
208 | || (sbconv = __ucs4_to_ibm930sb[ch + rp1->idx], \ |
209 | __builtin_expect (sbconv, L'\1') == L'\0' && ch != '\0')) \ | |
dbd10df7 UD |
210 | { \ |
211 | /* Use the UCS4 table for double byte. */ \ | |
212 | while (ch > rp2->end) \ | |
213 | ++rp2; \ | |
214 | \ | |
b9deb8ce | 215 | const char *cp; \ |
f1813b56 | 216 | if (__builtin_expect (ch < rp2->start, 0) \ |
dbd10df7 | 217 | || (cp = __ucs4_to_ibm930db[ch + rp2->idx], \ |
03a2c647 | 218 | __builtin_expect (cp[0], L'\1')== L'\0' && ch != '\0')) \ |
dbd10df7 UD |
219 | { \ |
220 | /* This is an illegal character. */ \ | |
e438a468 | 221 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
dbd10df7 UD |
222 | } \ |
223 | else \ | |
224 | { \ | |
f1813b56 | 225 | if (curcs == sb) \ |
dbd10df7 | 226 | { \ |
a1ffb40e | 227 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
228 | { \ |
229 | result = __GCONV_FULL_OUTPUT; \ | |
230 | break; \ | |
231 | } \ | |
232 | *outptr++ = SO; \ | |
233 | curcs = db; \ | |
234 | } \ | |
235 | \ | |
a1ffb40e | 236 | if (__glibc_unlikely (outptr + 2 > outend)) \ |
dbd10df7 UD |
237 | { \ |
238 | result = __GCONV_FULL_OUTPUT; \ | |
239 | break; \ | |
240 | } \ | |
241 | *outptr++ = cp[0]; \ | |
242 | *outptr++ = cp[1]; \ | |
243 | } \ | |
244 | } \ | |
245 | else \ | |
246 | { \ | |
247 | if (curcs == db) \ | |
248 | { \ | |
a1ffb40e | 249 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
250 | { \ |
251 | result = __GCONV_FULL_OUTPUT; \ | |
252 | break; \ | |
253 | } \ | |
254 | *outptr++ = SI; \ | |
42d6443f | 255 | curcs = sb; \ |
dbd10df7 UD |
256 | } \ |
257 | \ | |
a1ffb40e | 258 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
259 | { \ |
260 | result = __GCONV_FULL_OUTPUT; \ | |
261 | break; \ | |
262 | } \ | |
263 | if (ch == 0x7e) \ | |
264 | *outptr++ = 0xa1; \ | |
265 | else if (ch == 0x5c) \ | |
266 | *outptr++ = 0x5b; \ | |
267 | else \ | |
b9deb8ce | 268 | *outptr++ = sbconv; \ |
dbd10df7 UD |
269 | } \ |
270 | \ | |
271 | /* Now that we wrote the output increment the input pointer. */ \ | |
272 | inptr += 4; \ | |
273 | } | |
274 | #define LOOP_NEED_FLAGS | |
f1813b56 UD |
275 | #define EXTRA_LOOP_DECLS , int *curcsp |
276 | #define INIT_PARAMS int curcs = *curcsp & ~7 | |
4b1b449d | 277 | #define REINIT_PARAMS curcs = *curcsp & ~7 |
f1813b56 | 278 | #define UPDATE_PARAMS *curcsp = curcs |
dbd10df7 UD |
279 | #include <iconv/loop.c> |
280 | ||
281 | /* Now define the toplevel functions. */ | |
282 | #include <iconv/skeleton.c> |