1 /* Conversion from and to IBM930.
2 Copyright (C) 2000-2016 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/>. */
20 /* IBM930 is designed for the representation of Japanese Katakana/Kanji
21 using a stateful EBCDIC encoding scheme. It is also known as
22 CCSID 930 or CP930. See:
23 https://www-01.ibm.com/software/globalization/ccsid/ccsid930.html */
31 /* The shift sequences for this charset (it does not use ESC). */
32 #define SI 0x0F /* Shift In, host code to turn DBCS off. */
33 #define SO 0x0E /* Shift Out, host code to turn DBCS on. */
35 /* Definitions used in the body of the `gconv' function. */
36 #define CHARSET_NAME "IBM930//"
37 #define FROM_LOOP from_ibm930
38 #define TO_LOOP to_ibm930
39 #define ONE_DIRECTION 0
40 #define FROM_LOOP_MIN_NEEDED_FROM 1
41 #define FROM_LOOP_MAX_NEEDED_FROM 2
42 #define FROM_LOOP_MIN_NEEDED_TO 4
43 #define FROM_LOOP_MAX_NEEDED_TO 4
44 #define TO_LOOP_MIN_NEEDED_FROM 4
45 #define TO_LOOP_MAX_NEEDED_FROM 4
46 #define TO_LOOP_MIN_NEEDED_TO 1
47 #define TO_LOOP_MAX_NEEDED_TO 3
48 #define PREPARE_LOOP \
50 int *curcsp = &data->__statep->__count;
51 #define EXTRA_LOOP_ARGS , curcsp
53 /* Definitions of initialization and destructor function. */
58 /* Since this is a stateful encoding we have to provide code which resets
59 the output state to the initial state. This has to be done during the
61 #define EMIT_SHIFT_TO_INIT \
62 if ((data->__statep->__count & ~7) != sb) \
65 data->__statep->__count &= 7; \
68 /* We are not in the initial state. To switch back we have \
70 if (__glibc_unlikely (outbuf >= outend)) \
71 /* We don't have enough room in the output buffer. */ \
72 status = __GCONV_FULL_OUTPUT; \
75 /* Write out the shift sequence. */ \
77 data->__statep->__count &= 7; \
83 /* Since we might have to reset input pointer we must be able to save
84 and retore the state. */
85 #define SAVE_RESET_STATE(Save) \
87 save_curcs = *curcsp; \
92 /* Current codeset type. */
100 /* First, define the conversion function from IBM-930 to UCS4. */
101 #define MIN_NEEDED_INPUT FROM_LOOP_MIN_NEEDED_FROM
102 #define MAX_NEEDED_INPUT FROM_LOOP_MAX_NEEDED_FROM
103 #define MIN_NEEDED_OUTPUT FROM_LOOP_MIN_NEEDED_TO
104 #define MAX_NEEDED_OUTPUT FROM_LOOP_MAX_NEEDED_TO
105 #define LOOPFCT FROM_LOOP
108 uint32_t ch = *inptr; \
111 if (__builtin_expect (ch, 0) == SO) \
113 /* Shift OUT, change to DBCS converter. */ \
116 result = __GCONV_ILLEGAL_INPUT; \
123 else if (__builtin_expect (ch, 0) == SI) \
125 /* Shift IN, change to SBCS converter */ \
128 result = __GCONV_ILLEGAL_INPUT; \
138 /* Use the IBM930 table for single byte. */ \
139 res = __ibm930sb_to_ucs4[ch]; \
140 if (__builtin_expect (res, L'\1') == L'\0' && ch != '\0') \
142 /* This is an illegal character. */ \
143 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
147 put32 (outptr, res); \
154 /* Use the IBM930 table for double byte. */ \
155 const struct gap *rp2 = __ibm930db_to_ucs4_idx; \
157 assert (curcs == db); \
159 if (__glibc_unlikely (inptr + 1 >= inend)) \
161 /* The second character is not available. Store the \
162 intermediate result. */ \
163 result = __GCONV_INCOMPLETE_INPUT; \
167 ch = (ch * 0x100) + inptr[1]; \
168 while (ch > rp2->end) \
171 if (__builtin_expect (rp2->start == 0xffff, 0) \
172 || __builtin_expect (ch < rp2->start, 0) \
173 || (res = __ibm930db_to_ucs4[ch + rp2->idx], \
174 __builtin_expect (res, L'\1') == L'\0' && ch != '\0')) \
176 /* This is an illegal character. */ \
177 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
181 put32 (outptr, res); \
187 #define LOOP_NEED_FLAGS
188 #define EXTRA_LOOP_DECLS , int *curcsp
189 #define INIT_PARAMS int curcs = *curcsp & ~7
190 #define UPDATE_PARAMS *curcsp = curcs
191 #include <iconv/loop.c>
193 /* Next, define the other direction. */
194 #define MIN_NEEDED_INPUT TO_LOOP_MIN_NEEDED_FROM
195 #define MAX_NEEDED_INPUT TO_LOOP_MAX_NEEDED_FROM
196 #define MIN_NEEDED_OUTPUT TO_LOOP_MIN_NEEDED_TO
197 #define MAX_NEEDED_OUTPUT TO_LOOP_MAX_NEEDED_TO
198 #define LOOPFCT TO_LOOP
201 uint32_t ch = get32 (inptr); \
202 const struct gap *rp1 = __ucs4_to_ibm930sb_idx; \
203 const struct gap *rp2 = __ucs4_to_ibm930db_idx; \
206 if (__glibc_unlikely (ch >= 0xffff)) \
208 UNICODE_TAG_HANDLER (ch, 4); \
210 STANDARD_TO_LOOP_ERR_HANDLER (4); \
213 while (ch > rp1->end) \
216 /* Use the UCS4 table for single byte. */ \
217 if (__builtin_expect (ch < rp1->start, 0) \
218 || (cp = __ucs4_to_ibm930sb[ch + rp1->idx], \
219 __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0')) \
221 /* Use the UCS4 table for double byte. */ \
222 while (ch > rp2->end) \
225 if (__builtin_expect (ch < rp2->start, 0) \
226 || (cp = __ucs4_to_ibm930db[ch + rp2->idx], \
227 __builtin_expect (cp[0], L'\1')== L'\0' && ch != '\0')) \
229 /* This is an illegal character. */ \
230 STANDARD_TO_LOOP_ERR_HANDLER (4); \
236 if (__glibc_unlikely (outptr + 1 > outend)) \
238 result = __GCONV_FULL_OUTPUT; \
245 if (__glibc_unlikely (outptr + 2 > outend)) \
247 result = __GCONV_FULL_OUTPUT; \
258 if (__glibc_unlikely (outptr + 1 > outend)) \
260 result = __GCONV_FULL_OUTPUT; \
267 if (__glibc_unlikely (outptr + 1 > outend)) \
269 result = __GCONV_FULL_OUTPUT; \
274 else if (ch == 0x5c) \
280 /* Now that we wrote the output increment the input pointer. */ \
283 #define LOOP_NEED_FLAGS
284 #define EXTRA_LOOP_DECLS , int *curcsp
285 #define INIT_PARAMS int curcs = *curcsp & ~7
286 #define REINIT_PARAMS curcs = *curcsp & ~7
287 #define UPDATE_PARAMS *curcsp = curcs
288 #include <iconv/loop.c>
290 /* Now define the toplevel functions. */
291 #include <iconv/skeleton.c>