1 /* Conversion from and to IBM932.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Masahide Washizawa <washi@jp.ibm.com>, 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 <https://www.gnu.org/licenses/>. */
28 /* Definitions used in the body of the `gconv' function. */
29 #define CHARSET_NAME "IBM932//"
30 #define FROM_LOOP from_ibm932
31 #define TO_LOOP to_ibm932
32 #define ONE_DIRECTION 0
34 /* Definitions of initialization and destructor function. */
38 #define MIN_NEEDED_FROM 1
39 #define MAX_NEEDED_FROM 2
40 #define MIN_NEEDED_TO 4
42 /* First, define the conversion function from IBM-932 to UCS4. */
43 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
44 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
45 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
46 #define LOOPFCT FROM_LOOP
49 const struct gap *rp2 = __ibm932db_to_ucs4_idx; \
50 uint32_t ch = *inptr; \
53 if (__builtin_expect (ch == 0x80, 0) \
54 || __builtin_expect (ch == 0xa0, 0) \
55 || __builtin_expect (ch == 0xfd, 0) \
56 || __builtin_expect (ch == 0xfe, 0) \
57 || __builtin_expect (ch == 0xff, 0)) \
59 /* This is an illegal character. */ \
60 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
63 /* Use the IBM932 table for single byte. */ \
64 res = __ibm932sb_to_ucs4[ch]; \
65 if (__builtin_expect (res == 0, 0) && ch != 0) \
67 /* Use the IBM932 table for double byte. */ \
68 if (__glibc_unlikely (inptr + 1 >= inend)) \
70 /* The second character is not available. \
71 Store the intermediate result. */ \
72 result = __GCONV_INCOMPLETE_INPUT; \
76 ch = (ch * 0x100) + inptr[1]; \
77 /* ch was less than 0xfd. */ \
78 assert (ch < 0xfd00); \
79 while (ch > rp2->end) \
82 if (__builtin_expect (ch < rp2->start, 0) \
83 || (res = __ibm932db_to_ucs4[ch + rp2->idx], \
84 __builtin_expect (res, '\1') == 0 && ch !=0)) \
86 /* This is an illegal character. */ \
87 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
91 put32 (outptr, res); \
100 else if (res == 0x203e) \
102 put32 (outptr, res); \
107 #define LOOP_NEED_FLAGS
108 #define ONEBYTE_BODY \
110 if (c == 0x80 || c == 0xa0 || c >= 0xfd) \
112 uint32_t res = __ibm932sb_to_ucs4[c]; \
113 if (res == 0 && c != 0) \
117 else if (res == 0x203e) \
121 #include <iconv/loop.c>
123 /* Next, define the other direction. */
124 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
125 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
126 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
127 #define LOOPFCT TO_LOOP
130 const struct gap *rp = __ucs4_to_ibm932sb_idx; \
132 uint32_t ch = get32 (inptr); \
139 if (__glibc_unlikely (ch >= 0xffff)) \
141 UNICODE_TAG_HANDLER (ch, 4); \
145 while (ch > rp->end) \
148 /* Use the UCS4 table for single byte. */ \
149 if (__builtin_expect (rp == NULL, 0) \
150 || __builtin_expect (ch < rp->start, 0) \
151 || (sc = __ucs4_to_ibm932sb[ch + rp->idx], \
152 __builtin_expect (sc, '\1') == '\0' && ch != L'\0')) \
155 /* Use the UCS4 table for double byte. */ \
158 high = (sizeof (__ucs4_to_ibm932db) >> 1) \
159 / sizeof (__ucs4_to_ibm932db[0][FROM]); \
161 if (__glibc_likely (rp != NULL)) \
164 i = (low + high) >> 1; \
165 if (pccode < __ucs4_to_ibm932db[i][FROM]) \
167 else if (pccode > __ucs4_to_ibm932db[i][FROM]) \
171 pccode = __ucs4_to_ibm932db[i][TO]; \
178 if (__glibc_unlikely (outptr + 2 > outend)) \
180 result = __GCONV_FULL_OUTPUT; \
183 *outptr++ = pccode >> 8 & 0xff; \
184 *outptr++ = pccode & 0xff; \
188 /* This is an illegal character. */ \
189 STANDARD_TO_LOOP_ERR_HANDLER (4); \
194 if (__glibc_unlikely (outptr + 1 > outend)) \
196 result = __GCONV_FULL_OUTPUT; \
201 else if (ch == 0x7e) \
207 /* Now that we wrote the output increment the input pointer. */ \
210 #define LOOP_NEED_FLAGS
211 #include <iconv/loop.c>
213 /* Now define the toplevel functions. */
214 #include <iconv/skeleton.c>