1 /* Conversion from and to IBM933.
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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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 "IBM933//"
33 #define FROM_LOOP from_ibm933
34 #define TO_LOOP to_ibm933
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 \
41 int *curcsp = &data->__statep->__count;
42 #define EXTRA_LOOP_ARGS , curcsp
44 /* Definitions of initialization and destructor function. */
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
52 #define EMIT_SHIFT_TO_INIT \
53 if ((data->__statep->__count & ~7) != sb) \
56 data->__statep->__count &= 7; \
59 unsigned char *outbuf = data->__outbuf; \
61 /* We are not in the initial state. To switch back we have \
63 if (__builtin_expect (outbuf >= data->__outbufend, 0)) \
64 /* We don't have enough room in the output buffer. */ \
65 status = __GCONV_FULL_OUTPUT; \
68 /* Write out the shift sequence. */ \
70 data->__outbuf = outbuf; \
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-933 to UCS4. */
94 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
95 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
96 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
97 #define LOOPFCT FROM_LOOP
100 uint32_t ch = *inptr; \
103 if (__builtin_expect (ch, 0) == SO) \
105 /* Shift OUT, change to DBCS converter. */ \
108 result = __GCONV_ILLEGAL_INPUT; \
115 else if (__builtin_expect (ch, 0) == SI) \
117 /* Shift IN, change to SBCS converter. */ \
120 result = __GCONV_ILLEGAL_INPUT; \
130 /* Use the IBM933 table for single byte. */ \
131 res = __ibm933sb_to_ucs4[ch]; \
132 if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \
134 /* This is an illegal character. */ \
135 if (! ignore_errors_p ()) \
137 result = __GCONV_ILLEGAL_INPUT; \
144 put32 (outptr, res); \
151 const struct gap *rp2 = __ibm933db_to_ucs4_idx; \
153 assert (curcs == db); \
155 /* Use the IBM933 table for double byte. */ \
156 if (__builtin_expect (inptr + 1 >= inend, 0)) \
158 /* The second character is not available. Store the \
159 intermediate result. */ \
160 result = __GCONV_INCOMPLETE_INPUT; \
164 ch = (ch * 0x100) + inptr[1]; \
165 while (ch > rp2->end) \
168 if (__builtin_expect (rp2 == NULL, 0) \
169 || __builtin_expect (ch < rp2->start, 0) \
170 || (res = __ibm933db_to_ucs4[ch + rp2->idx], \
171 __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \
173 /* This is an illegal character. */ \
174 if (! ignore_errors_p ()) \
176 result = __GCONV_ILLEGAL_INPUT; \
185 put32 (outptr, res); \
191 #define LOOP_NEED_FLAGS
192 #define EXTRA_LOOP_DECLS , int *curcsp
193 #define INIT_PARAMS int curcs = *curcsp & ~7
194 #define UPDATE_PARAMS *curcsp = curcs
195 #include <iconv/loop.c>
197 /* Next, define the other direction. */
198 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
199 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
200 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
201 #define LOOPFCT TO_LOOP
204 uint32_t ch = get32 (inptr); \
205 const struct gap *rp1 = __ucs4_to_ibm933sb_idx; \
206 const struct gap *rp2 = __ucs4_to_ibm933db_idx; \
209 if (__builtin_expect (ch, 0) >= 0xffff) \
211 if (! ignore_errors_p ()) \
213 result = __GCONV_ILLEGAL_INPUT; \
221 while (ch > rp1->end) \
224 /* Use the UCS4 table for single byte. */ \
225 if (__builtin_expect (ch < rp1->start, 0) \
226 || (cp = __ucs4_to_ibm933sb[ch + rp1->idx], \
227 __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0')) \
229 /* Use the UCS4 table for double byte. */ \
230 while (ch > rp2->end) \
233 if (__builtin_expect (ch < rp2->start, 0) \
234 || (cp = __ucs4_to_ibm933db[ch + rp2->idx], \
235 __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0')) \
237 /* This is an illegal character. */ \
238 if (! ignore_errors_p ()) \
240 result = __GCONV_ILLEGAL_INPUT; \
249 if (__builtin_expect (outptr+1 > outend, 0)) \
251 result = __GCONV_FULL_OUTPUT; \
258 if (__builtin_expect (outptr + 2 > outend, 0)) \
260 result = __GCONV_FULL_OUTPUT; \
271 if (__builtin_expect (outptr + 1 > outend, 0)) \
273 result = __GCONV_FULL_OUTPUT; \
279 if (__builtin_expect (outptr + 1 > outend, 0)) \
281 result = __GCONV_FULL_OUTPUT; \
288 /* Now that we wrote the output increment the input pointer. */ \
291 #define LOOP_NEED_FLAGS
292 #define EXTRA_LOOP_DECLS , int *curcsp
293 #define INIT_PARAMS int curcs = *curcsp & ~7
294 #define UPDATE_PARAMS *curcsp = curcs
295 #include <iconv/loop.c>
297 /* Now define the toplevel functions. */
298 #include <iconv/skeleton.c>