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