1 /* Conversion to and from IBM939.
2 Copyright (C) 2000-2002, 2005, 2008 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, see
18 <http://www.gnu.org/licenses/>. */
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. */
30 /* Definitions used in the body of the `gconv' function. */
31 #define CHARSET_NAME "IBM939//"
32 #define FROM_LOOP from_ibm939
33 #define TO_LOOP to_ibm939
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
42 #define PREPARE_LOOP \
44 int *curcsp = &data->__statep->__count;
45 #define EXTRA_LOOP_ARGS , curcsp
47 /* Definitions of initialization and destructor function. */
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
55 #define EMIT_SHIFT_TO_INIT \
56 if ((data->__statep->__count & ~7) != sb) \
59 data->__statep->__count &= 7; \
62 /* We are not in the initial state. To switch back we have \
64 if (__builtin_expect (outbuf >= outend, 0)) \
65 /* We don't have enough room in the output buffer. */ \
66 status = __GCONV_FULL_OUTPUT; \
69 /* Write out the shift sequence. */ \
71 data->__statep->__count &= 7; \
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) \
81 save_curcs = *curcsp; \
86 /* Current codeset type. */
93 /* First, define the conversion function from IBM-939 to UCS4. */
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
98 #define LOOPFCT FROM_LOOP
101 uint32_t ch = *inptr; \
104 if (__builtin_expect (ch, 0) == SO) \
106 /* Shift OUT, change to DBCS converter. */ \
109 result = __GCONV_ILLEGAL_INPUT; \
116 else if (__builtin_expect (ch, 0) == SI) \
118 /* Shift IN, change to SBCS converter. */ \
121 result = __GCONV_ILLEGAL_INPUT; \
131 /* Use the IBM939 table for single byte. */ \
132 res = __ibm939sb_to_ucs4[ch]; \
133 if (__builtin_expect (res == L'\0', 0) && ch != '\0') \
135 /* This is an illegal character. */ \
136 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
140 put32 (outptr, res); \
147 /* Use the IBM939 table for double byte. */ \
148 const struct gap *rp2 = __ibm939db_to_ucs4_idx; \
150 assert (curcs == db); \
152 if (__builtin_expect (inptr + 1 >= inend, 0)) \
154 /* The second character is not available. Store the \
155 intermediate result. */ \
156 result = __GCONV_INCOMPLETE_INPUT; \
160 ch = (ch * 0x100) + inptr[1]; \
161 while (ch > rp2->end) \
164 if (__builtin_expect (rp2 == NULL, 0) \
165 || __builtin_expect (ch < rp2->start, 0) \
166 || (res = __ibm939db_to_ucs4[ch + rp2->idx], \
167 __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \
169 /* This is an illegal character. */ \
170 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
174 put32 (outptr, res); \
180 #define LOOP_NEED_FLAGS
181 #define EXTRA_LOOP_DECLS , int *curcsp
182 #define INIT_PARAMS int curcs = *curcsp & ~7
183 #define UPDATE_PARAMS *curcsp = curcs
184 #include <iconv/loop.c>
186 /* Next, define the other direction */
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
191 #define LOOPFCT TO_LOOP
194 uint32_t ch = get32 (inptr); \
195 const struct gap *rp1 = __ucs4_to_ibm939sb_idx; \
196 const struct gap *rp2 = __ucs4_to_ibm939db_idx; \
199 if (__builtin_expect (ch >= 0xffff, 0)) \
201 UNICODE_TAG_HANDLER (ch, 4); \
202 goto ibm939_invalid_char; \
205 while (ch > rp1->end) \
208 /* Use the UCS4 table for single byte. */ \
209 if (__builtin_expect (ch < rp1->start, 0) \
210 || (cp = __ucs4_to_ibm939sb[ch + rp1->idx], \
211 __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0')) \
213 /* Use the UCS4 table for double byte. */ \
214 while (ch > rp2->end) \
217 if (__builtin_expect (ch < rp2->start, 0) \
218 || (cp = __ucs4_to_ibm939db[ch + rp2->idx], \
219 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0')) \
221 /* This is an illegal character. */ \
222 ibm939_invalid_char: \
223 STANDARD_TO_LOOP_ERR_HANDLER (4); \
229 if (__builtin_expect (outptr + 1 > outend, 0)) \
231 result = __GCONV_FULL_OUTPUT; \
238 if (__builtin_expect (outptr + 2 > outend, 0)) \
240 result = __GCONV_FULL_OUTPUT; \
251 if (__builtin_expect (outptr + 1 > outend, 0)) \
253 result = __GCONV_FULL_OUTPUT; \
259 if (__builtin_expect (outptr + 1 > outend, 0)) \
261 result = __GCONV_FULL_OUTPUT; \
266 else if (ch == 0x5c) \
273 /* Now that we wrote the output increment the input pointer. */ \
276 #define LOOP_NEED_FLAGS
277 #define EXTRA_LOOP_DECLS , int *curcsp
278 #define INIT_PARAMS int curcs = *curcsp & ~7
279 #define REINIT_PARAMS curcs = *curcsp & ~7
280 #define UPDATE_PARAMS *curcsp = curcs
281 #include <iconv/loop.c>
283 /* Now define the toplevel functions. */
284 #include <iconv/skeleton.c>