]>
Commit | Line | Data |
---|---|---|
dbd10df7 | 1 | /* Conversion from and to IBM930. |
d4697bc9 | 2 | Copyright (C) 2000-2014 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 UD |
19 | |
20 | #include <dlfcn.h> | |
21 | #include <stdint.h> | |
22 | #include <wchar.h> | |
23 | #include <byteswap.h> | |
24 | #include "ibm930.h" | |
25 | ||
26 | /* The shift sequences for this charset (it does not use ESC). */ | |
27 | #define SI 0x0F /* Shift In, host code to turn DBCS off. */ | |
28 | #define SO 0x0E /* Shift Out, host code to turn DBCS on. */ | |
29 | ||
30 | /* Definitions used in the body of the `gconv' function. */ | |
31 | #define CHARSET_NAME "IBM930//" | |
32 | #define FROM_LOOP from_ibm930 | |
33 | #define TO_LOOP to_ibm930 | |
13e402e7 | 34 | #define ONE_DIRECTION 0 |
faaa6f62 UD |
35 | #define FROM_LOOP_MIN_NEEDED_FROM 1 |
36 | #define FROM_LOOP_MAX_NEEDED_FROM 2 | |
37 | #define FROM_LOOP_MIN_NEEDED_TO 4 | |
38 | #define FROM_LOOP_MAX_NEEDED_TO 4 | |
39 | #define TO_LOOP_MIN_NEEDED_FROM 4 | |
40 | #define TO_LOOP_MAX_NEEDED_FROM 4 | |
41 | #define TO_LOOP_MIN_NEEDED_TO 1 | |
42 | #define TO_LOOP_MAX_NEEDED_TO 3 | |
f1813b56 UD |
43 | #define PREPARE_LOOP \ |
44 | int save_curcs; \ | |
45 | int *curcsp = &data->__statep->__count; | |
46 | #define EXTRA_LOOP_ARGS , curcsp | |
dbd10df7 UD |
47 | |
48 | /* Definitions of initialization and destructor function. */ | |
49 | #define DEFINE_INIT 1 | |
50 | #define DEFINE_FINI 1 | |
51 | ||
f1813b56 UD |
52 | |
53 | /* Since this is a stateful encoding we have to provide code which resets | |
54 | the output state to the initial state. This has to be done during the | |
55 | flushing. */ | |
56 | #define EMIT_SHIFT_TO_INIT \ | |
57 | if ((data->__statep->__count & ~7) != sb) \ | |
58 | { \ | |
59 | if (FROM_DIRECTION) \ | |
60 | data->__statep->__count &= 7; \ | |
61 | else \ | |
62 | { \ | |
f1813b56 UD |
63 | /* We are not in the initial state. To switch back we have \ |
64 | to emit `SI'. */ \ | |
a1ffb40e | 65 | if (__glibc_unlikely (outbuf >= outend)) \ |
f1813b56 UD |
66 | /* We don't have enough room in the output buffer. */ \ |
67 | status = __GCONV_FULL_OUTPUT; \ | |
68 | else \ | |
69 | { \ | |
70 | /* Write out the shift sequence. */ \ | |
71 | *outbuf++ = SI; \ | |
f1813b56 UD |
72 | data->__statep->__count &= 7; \ |
73 | } \ | |
74 | } \ | |
75 | } | |
76 | ||
77 | ||
78 | /* Since we might have to reset input pointer we must be able to save | |
79 | and retore the state. */ | |
80 | #define SAVE_RESET_STATE(Save) \ | |
81 | if (Save) \ | |
82 | save_curcs = *curcsp; \ | |
83 | else \ | |
84 | *curcsp = save_curcs | |
85 | ||
dbd10df7 UD |
86 | |
87 | /* Current codeset type. */ | |
88 | enum | |
89 | { | |
f1813b56 UD |
90 | sb = 0, |
91 | db = 64 | |
dbd10df7 UD |
92 | }; |
93 | ||
f1813b56 | 94 | |
dbd10df7 | 95 | /* First, define the conversion function from IBM-930 to UCS4. */ |
faaa6f62 UD |
96 | #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM |
97 | #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM | |
98 | #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO | |
99 | #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO | |
dbd10df7 UD |
100 | #define LOOPFCT FROM_LOOP |
101 | #define BODY \ | |
102 | { \ | |
103 | uint32_t ch = *inptr; \ | |
104 | uint32_t res; \ | |
dbd10df7 UD |
105 | \ |
106 | if (__builtin_expect (ch, 0) == SO) \ | |
107 | { \ | |
dbd10df7 UD |
108 | /* Shift OUT, change to DBCS converter. */ \ |
109 | if (curcs == db) \ | |
110 | { \ | |
111 | result = __GCONV_ILLEGAL_INPUT; \ | |
112 | break; \ | |
113 | } \ | |
114 | curcs = db; \ | |
115 | ++inptr; \ | |
f1813b56 | 116 | continue; \ |
dbd10df7 UD |
117 | } \ |
118 | else if (__builtin_expect (ch, 0) == SI) \ | |
119 | { \ | |
dbd10df7 UD |
120 | /* Shift IN, change to SBCS converter */ \ |
121 | if (curcs == sb) \ | |
122 | { \ | |
123 | result = __GCONV_ILLEGAL_INPUT; \ | |
124 | break; \ | |
125 | } \ | |
126 | curcs = sb; \ | |
127 | ++inptr; \ | |
f1813b56 | 128 | continue; \ |
dbd10df7 UD |
129 | } \ |
130 | \ | |
f1813b56 | 131 | if (curcs == sb) \ |
dbd10df7 UD |
132 | { \ |
133 | /* Use the IBM930 table for single byte. */ \ | |
f1813b56 UD |
134 | res = __ibm930sb_to_ucs4[ch]; \ |
135 | if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \ | |
dbd10df7 UD |
136 | { \ |
137 | /* This is an illegal character. */ \ | |
e438a468 | 138 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
dbd10df7 UD |
139 | } \ |
140 | else \ | |
141 | { \ | |
dbd10df7 UD |
142 | put32 (outptr, res); \ |
143 | outptr += 4; \ | |
dbd10df7 | 144 | } \ |
f1813b56 | 145 | ++inptr; \ |
dbd10df7 | 146 | } \ |
f1813b56 | 147 | else \ |
dbd10df7 UD |
148 | { \ |
149 | /* Use the IBM930 table for double byte. */ \ | |
f1813b56 UD |
150 | const struct gap *rp2 = __ibm930db_to_ucs4_idx; \ |
151 | \ | |
152 | assert (curcs == db); \ | |
153 | \ | |
a1ffb40e | 154 | if (__glibc_unlikely (inptr + 1 >= inend)) \ |
dbd10df7 UD |
155 | { \ |
156 | /* The second character is not available. Store the \ | |
157 | intermediate result. */ \ | |
158 | result = __GCONV_INCOMPLETE_INPUT; \ | |
159 | break; \ | |
160 | } \ | |
161 | \ | |
162 | ch = (ch * 0x100) + inptr[1]; \ | |
163 | while (ch > rp2->end) \ | |
164 | ++rp2; \ | |
165 | \ | |
6e230d11 SP |
166 | if (__builtin_expect (rp2->start == 0xffff, 0) \ |
167 | || __builtin_expect (ch < rp2->start, 0) \ | |
dbd10df7 UD |
168 | || (res = __ibm930db_to_ucs4[ch + rp2->idx], \ |
169 | __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \ | |
170 | { \ | |
171 | /* This is an illegal character. */ \ | |
e438a468 | 172 | STANDARD_FROM_LOOP_ERR_HANDLER (2); \ |
dbd10df7 UD |
173 | } \ |
174 | else \ | |
175 | { \ | |
176 | put32 (outptr, res); \ | |
177 | outptr += 4; \ | |
dbd10df7 | 178 | } \ |
f1813b56 | 179 | inptr += 2; \ |
dbd10df7 UD |
180 | } \ |
181 | } | |
182 | #define LOOP_NEED_FLAGS | |
f1813b56 UD |
183 | #define EXTRA_LOOP_DECLS , int *curcsp |
184 | #define INIT_PARAMS int curcs = *curcsp & ~7 | |
185 | #define UPDATE_PARAMS *curcsp = curcs | |
dbd10df7 UD |
186 | #include <iconv/loop.c> |
187 | ||
188 | /* Next, define the other direction. */ | |
faaa6f62 UD |
189 | #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM |
190 | #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM | |
191 | #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO | |
192 | #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO | |
dbd10df7 UD |
193 | #define LOOPFCT TO_LOOP |
194 | #define BODY \ | |
195 | { \ | |
196 | uint32_t ch = get32 (inptr); \ | |
197 | const struct gap *rp1 = __ucs4_to_ibm930sb_idx; \ | |
198 | const struct gap *rp2 = __ucs4_to_ibm930db_idx; \ | |
199 | const char *cp; \ | |
200 | \ | |
a1ffb40e | 201 | if (__glibc_unlikely (ch >= 0xffff)) \ |
dbd10df7 | 202 | { \ |
601d2942 UD |
203 | UNICODE_TAG_HANDLER (ch, 4); \ |
204 | \ | |
e438a468 | 205 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
dbd10df7 | 206 | } \ |
f1813b56 UD |
207 | \ |
208 | while (ch > rp1->end) \ | |
209 | ++rp1; \ | |
dbd10df7 UD |
210 | \ |
211 | /* Use the UCS4 table for single byte. */ \ | |
faaa6f62 | 212 | if (__builtin_expect (ch < rp1->start, 0) \ |
dbd10df7 UD |
213 | || (cp = __ucs4_to_ibm930sb[ch + rp1->idx], \ |
214 | __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0')) \ | |
215 | { \ | |
216 | /* Use the UCS4 table for double byte. */ \ | |
217 | while (ch > rp2->end) \ | |
218 | ++rp2; \ | |
219 | \ | |
f1813b56 | 220 | if (__builtin_expect (ch < rp2->start, 0) \ |
dbd10df7 | 221 | || (cp = __ucs4_to_ibm930db[ch + rp2->idx], \ |
03a2c647 | 222 | __builtin_expect (cp[0], L'\1')== L'\0' && ch != '\0')) \ |
dbd10df7 UD |
223 | { \ |
224 | /* This is an illegal character. */ \ | |
e438a468 | 225 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
dbd10df7 UD |
226 | } \ |
227 | else \ | |
228 | { \ | |
f1813b56 | 229 | if (curcs == sb) \ |
dbd10df7 | 230 | { \ |
a1ffb40e | 231 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
232 | { \ |
233 | result = __GCONV_FULL_OUTPUT; \ | |
234 | break; \ | |
235 | } \ | |
236 | *outptr++ = SO; \ | |
237 | curcs = db; \ | |
238 | } \ | |
239 | \ | |
a1ffb40e | 240 | if (__glibc_unlikely (outptr + 2 > outend)) \ |
dbd10df7 UD |
241 | { \ |
242 | result = __GCONV_FULL_OUTPUT; \ | |
243 | break; \ | |
244 | } \ | |
245 | *outptr++ = cp[0]; \ | |
246 | *outptr++ = cp[1]; \ | |
247 | } \ | |
248 | } \ | |
249 | else \ | |
250 | { \ | |
251 | if (curcs == db) \ | |
252 | { \ | |
a1ffb40e | 253 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
254 | { \ |
255 | result = __GCONV_FULL_OUTPUT; \ | |
256 | break; \ | |
257 | } \ | |
258 | *outptr++ = SI; \ | |
259 | } \ | |
260 | \ | |
a1ffb40e | 261 | if (__glibc_unlikely (outptr + 1 > outend)) \ |
dbd10df7 UD |
262 | { \ |
263 | result = __GCONV_FULL_OUTPUT; \ | |
264 | break; \ | |
265 | } \ | |
266 | if (ch == 0x7e) \ | |
267 | *outptr++ = 0xa1; \ | |
268 | else if (ch == 0x5c) \ | |
269 | *outptr++ = 0x5b; \ | |
270 | else \ | |
271 | *outptr++ = cp[0]; \ | |
272 | curcs = sb; \ | |
273 | } \ | |
274 | \ | |
275 | /* Now that we wrote the output increment the input pointer. */ \ | |
276 | inptr += 4; \ | |
277 | } | |
278 | #define LOOP_NEED_FLAGS | |
f1813b56 UD |
279 | #define EXTRA_LOOP_DECLS , int *curcsp |
280 | #define INIT_PARAMS int curcs = *curcsp & ~7 | |
4b1b449d | 281 | #define REINIT_PARAMS curcs = *curcsp & ~7 |
f1813b56 | 282 | #define UPDATE_PARAMS *curcsp = curcs |
dbd10df7 UD |
283 | #include <iconv/loop.c> |
284 | ||
285 | /* Now define the toplevel functions. */ | |
286 | #include <iconv/skeleton.c> |