* sysdeps/unix/sysv/linux/configure.in
[glibc.git] / iconvdata / ibm930.c
blobf511e48ae2ab1396d025a2b04c1a2100494e6a12
1 /* Conversion from and to IBM930.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Masahide Washizawa <washi@yamato.ibm.co.jp>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <dlfcn.h>
22 #include <stdint.h>
23 #include <wchar.h>
24 #include <byteswap.h>
25 #include "ibm930.h"
27 /* The shift sequences for this charset (it does not use ESC). */
28 #define SI 0x0F /* Shift In, host code to turn DBCS off. */
29 #define SO 0x0E /* Shift Out, host code to turn DBCS on. */
31 /* Definitions used in the body of the `gconv' function. */
32 #define CHARSET_NAME "IBM930//"
33 #define FROM_LOOP from_ibm930
34 #define TO_LOOP to_ibm930
35 #define MIN_NEEDED_FROM 1
36 #define MAX_NEEDED_FROM 2
37 #define MIN_NEEDED_TO 4
38 #define MAX_NEEDED_TO 4
39 #define PREPARE_LOOP \
40 int save_curcs; \
41 int *curcsp = &data->__statep->__count;
42 #define EXTRA_LOOP_ARGS , curcsp
44 /* Definitions of initialization and destructor function. */
45 #define DEFINE_INIT 1
46 #define DEFINE_FINI 1
49 /* Since this is a stateful encoding we have to provide code which resets
50 the output state to the initial state. This has to be done during the
51 flushing. */
52 #define EMIT_SHIFT_TO_INIT \
53 if ((data->__statep->__count & ~7) != sb) \
54 { \
55 if (FROM_DIRECTION) \
56 data->__statep->__count &= 7; \
57 else \
58 { \
59 /* We are not in the initial state. To switch back we have \
60 to emit `SI'. */ \
61 if (__builtin_expect (outbuf >= outend, 0)) \
62 /* We don't have enough room in the output buffer. */ \
63 status = __GCONV_FULL_OUTPUT; \
64 else \
65 { \
66 /* Write out the shift sequence. */ \
67 *outbuf++ = SI; \
68 data->__statep->__count &= 7; \
69 } \
70 } \
74 /* Since we might have to reset input pointer we must be able to save
75 and retore the state. */
76 #define SAVE_RESET_STATE(Save) \
77 if (Save) \
78 save_curcs = *curcsp; \
79 else \
80 *curcsp = save_curcs
83 /* Current codeset type. */
84 enum
86 sb = 0,
87 db = 64
91 /* First, define the conversion function from IBM-930 to UCS4. */
92 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
93 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
94 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
95 #define LOOPFCT FROM_LOOP
96 #define BODY \
97 { \
98 uint32_t ch = *inptr; \
99 uint32_t res; \
101 if (__builtin_expect (ch, 0) == SO) \
103 /* Shift OUT, change to DBCS converter. */ \
104 if (curcs == db) \
106 result = __GCONV_ILLEGAL_INPUT; \
107 break; \
109 curcs = db; \
110 ++inptr; \
111 continue; \
113 else if (__builtin_expect (ch, 0) == SI) \
115 /* Shift IN, change to SBCS converter */ \
116 if (curcs == sb) \
118 result = __GCONV_ILLEGAL_INPUT; \
119 break; \
121 curcs = sb; \
122 ++inptr; \
123 continue; \
126 if (curcs == sb) \
128 /* Use the IBM930 table for single byte. */ \
129 res = __ibm930sb_to_ucs4[ch]; \
130 if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \
132 /* This is an illegal character. */ \
133 if (! ignore_errors_p ()) \
135 result = __GCONV_ILLEGAL_INPUT; \
136 break; \
138 ++*irreversible; \
140 else \
142 put32 (outptr, res); \
143 outptr += 4; \
145 ++inptr; \
147 else \
149 /* Use the IBM930 table for double byte. */ \
150 const struct gap *rp2 = __ibm930db_to_ucs4_idx; \
152 assert (curcs == db); \
154 if (__builtin_expect (inptr + 1 >= inend, 0)) \
156 /* The second character is not available. Store the \
157 intermediate result. */ \
158 result = __GCONV_INCOMPLETE_INPUT; \
159 break; \
162 ch = (ch * 0x100) + inptr[1]; \
163 while (ch > rp2->end) \
164 ++rp2; \
166 if (__builtin_expect (ch < rp2->start, 0) \
167 || (res = __ibm930db_to_ucs4[ch + rp2->idx], \
168 __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \
170 /* This is an illegal character. */ \
171 if (! ignore_errors_p ()) \
173 result = __GCONV_ILLEGAL_INPUT; \
174 break; \
176 ++*irreversible; \
178 else \
180 put32 (outptr, res); \
181 outptr += 4; \
183 inptr += 2; \
186 #define LOOP_NEED_FLAGS
187 #define EXTRA_LOOP_DECLS , int *curcsp
188 #define INIT_PARAMS int curcs = *curcsp & ~7
189 #define UPDATE_PARAMS *curcsp = curcs
190 #include <iconv/loop.c>
192 /* Next, define the other direction. */
193 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
194 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
195 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
196 #define LOOPFCT TO_LOOP
197 #define BODY \
199 uint32_t ch = get32 (inptr); \
200 const struct gap *rp1 = __ucs4_to_ibm930sb_idx; \
201 const struct gap *rp2 = __ucs4_to_ibm930db_idx; \
202 const char *cp; \
204 if (__builtin_expect (ch >= 0xffff, 0)) \
206 UNICODE_TAG_HANDLER (ch, 4); \
208 if (! ignore_errors_p ()) \
210 result = __GCONV_ILLEGAL_INPUT; \
211 break; \
213 ++*irreversible; \
214 inptr += 4; \
215 continue; \
218 while (ch > rp1->end) \
219 ++rp1; \
221 /* Use the UCS4 table for single byte. */ \
222 if (__builtin_expect (ch < rp1->start, 0) \
223 || (cp = __ucs4_to_ibm930sb[ch + rp1->idx], \
224 __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0')) \
226 /* Use the UCS4 table for double byte. */ \
227 while (ch > rp2->end) \
228 ++rp2; \
230 if (__builtin_expect (ch < rp2->start, 0) \
231 || (cp = __ucs4_to_ibm930db[ch + rp2->idx], \
232 __builtin_expect (cp[0], L'\1')== L'\0' && ch != '\0')) \
234 /* This is an illegal character. */ \
235 if (! ignore_errors_p ()) \
237 result = __GCONV_ILLEGAL_INPUT; \
238 break; \
240 ++*irreversible; \
242 else \
244 if (curcs == sb) \
246 if (__builtin_expect (outptr + 1 > outend, 0)) \
248 result = __GCONV_FULL_OUTPUT; \
249 break; \
251 *outptr++ = SO; \
252 curcs = db; \
255 if (__builtin_expect (outptr + 2 > outend, 0)) \
257 result = __GCONV_FULL_OUTPUT; \
258 break; \
260 *outptr++ = cp[0]; \
261 *outptr++ = cp[1]; \
264 else \
266 if (curcs == db) \
268 if (__builtin_expect (outptr + 1 > outend, 0)) \
270 result = __GCONV_FULL_OUTPUT; \
271 break; \
273 *outptr++ = SI; \
276 if (__builtin_expect (outptr + 1 > outend, 0)) \
278 result = __GCONV_FULL_OUTPUT; \
279 break; \
281 if (ch == 0x7e) \
282 *outptr++ = 0xa1; \
283 else if (ch == 0x5c) \
284 *outptr++ = 0x5b; \
285 else \
286 *outptr++ = cp[0]; \
287 curcs = sb; \
290 /* Now that we wrote the output increment the input pointer. */ \
291 inptr += 4; \
293 #define LOOP_NEED_FLAGS
294 #define EXTRA_LOOP_DECLS , int *curcsp
295 #define INIT_PARAMS int curcs = *curcsp & ~7
296 #define UPDATE_PARAMS *curcsp = curcs
297 #include <iconv/loop.c>
299 /* Now define the toplevel functions. */
300 #include <iconv/skeleton.c>