1 /* Conversion from and to IBM932.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 /* Definitions used in the body of the `gconv' function. */
28 #define CHARSET_NAME "IBM932//"
29 #define FROM_LOOP from_ibm932
30 #define TO_LOOP to_ibm932
31 #define ONE_DIRECTION 0
33 /* Definitions of initialization and destructor function. */
37 #define MIN_NEEDED_FROM 1
38 #define MAX_NEEDED_FROM 2
39 #define MIN_NEEDED_TO 4
41 /* First, define the conversion function from IBM-932 to UCS4. */
42 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
43 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
44 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
45 #define LOOPFCT FROM_LOOP
48 const struct gap *rp2 = __ibm932db_to_ucs4_idx; \
49 uint32_t ch = *inptr; \
52 if (__builtin_expect (ch == 0x80, 0) \
53 || __builtin_expect (ch == 0xa0, 0) \
54 || __builtin_expect (ch == 0xfd, 0) \
55 || __builtin_expect (ch == 0xfe, 0) \
56 || __builtin_expect (ch == 0xff, 0)) \
58 /* This is an illegal character. */ \
59 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
62 /* Use the IBM932 table for single byte. */ \
63 res = __ibm932sb_to_ucs4[ch]; \
64 if (__builtin_expect (res == 0, 0) && ch != 0) \
66 /* Use the IBM932 table for double byte. */ \
67 if (__glibc_unlikely (inptr + 1 >= inend)) \
69 /* The second character is not available. \
70 Store the intermediate result. */ \
71 result = __GCONV_INCOMPLETE_INPUT; \
75 ch = (ch * 0x100) + inptr[1]; \
76 /* ch was less than 0xfd. */ \
77 assert (ch < 0xfd00); \
78 while (ch > rp2->end) \
81 if (__builtin_expect (ch < rp2->start, 0) \
82 || (res = __ibm932db_to_ucs4[ch + rp2->idx], \
83 __builtin_expect (res, '\1') == 0 && ch !=0)) \
85 /* This is an illegal character. */ \
86 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
90 put32 (outptr, res); \
99 else if (res == 0x203e) \
101 put32 (outptr, res); \
106 #define LOOP_NEED_FLAGS
107 #define ONEBYTE_BODY \
109 if (c == 0x80 || c == 0xa0 || c >= 0xfd) \
111 uint32_t res = __ibm932sb_to_ucs4[c]; \
112 if (res == 0 && c != 0) \
116 else if (res == 0x203e) \
120 #include <iconv/loop.c>
122 /* Next, define the other direction. */
123 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
124 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
125 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
126 #define LOOPFCT TO_LOOP
129 const struct gap *rp = __ucs4_to_ibm932sb_idx; \
131 uint32_t ch = get32 (inptr); \
138 if (__glibc_unlikely (ch >= 0xffff)) \
140 UNICODE_TAG_HANDLER (ch, 4); \
144 while (ch > rp->end) \
147 /* Use the UCS4 table for single byte. */ \
148 if (__builtin_expect (rp == NULL, 0) \
149 || __builtin_expect (ch < rp->start, 0) \
150 || (sc = __ucs4_to_ibm932sb[ch + rp->idx], \
151 __builtin_expect (sc, '\1') == '\0' && ch != L'\0')) \
154 /* Use the UCS4 table for double byte. */ \
157 high = (sizeof (__ucs4_to_ibm932db) >> 1) \
158 / sizeof (__ucs4_to_ibm932db[0][FROM]); \
160 if (__glibc_likely (rp != NULL)) \
163 i = (low + high) >> 1; \
164 if (pccode < __ucs4_to_ibm932db[i][FROM]) \
166 else if (pccode > __ucs4_to_ibm932db[i][FROM]) \
170 pccode = __ucs4_to_ibm932db[i][TO]; \
177 if (__glibc_unlikely (outptr + 2 > outend)) \
179 result = __GCONV_FULL_OUTPUT; \
182 *outptr++ = pccode >> 8 & 0xff; \
183 *outptr++ = pccode & 0xff; \
187 /* This is an illegal character. */ \
188 STANDARD_TO_LOOP_ERR_HANDLER (4); \
193 if (__glibc_unlikely (outptr + 1 > outend)) \
195 result = __GCONV_FULL_OUTPUT; \
200 else if (ch == 0x7e) \
206 /* Now that we wrote the output increment the input pointer. */ \
209 #define LOOP_NEED_FLAGS
210 #include <iconv/loop.c>
212 /* Now define the toplevel functions. */
213 #include <iconv/skeleton.c>