Update.
[glibc.git] / iconvdata / ibm932.c
blob05d50ad86f02d70c19327e6b9db29553e1d96e8c
1 /* Conversion from and to IBM932.
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@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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include "ibm932.h"
23 #ifndef TRUE
24 #define TRUE 1
25 #define FALSE 0
26 #endif
28 #define FROM 0
29 #define TO 1
31 /* Definitions used in the body of the `gconv' function. */
32 #define CHARSET_NAME "IBM932//"
33 #define FROM_LOOP from_ibm932
34 #define TO_LOOP to_ibm932
36 /* Definitions of initialization and destructor function. */
37 #define DEFINE_INIT 1
38 #define DEFINE_FINI 1
40 #define MIN_NEEDED_FROM 1
41 #define MAX_NEEDED_FROM 2
42 #define MIN_NEEDED_TO 4
44 /* First, define the conversion function from IBM-932 to UCS4. */
45 #define MIN_NEEDED_INPUT MIN_NEEDED_FROM
46 #define MAX_NEEDED_INPUT MAX_NEEDED_FROM
47 #define MIN_NEEDED_OUTPUT MIN_NEEDED_TO
48 #define LOOPFCT FROM_LOOP
49 #define BODY \
50 { \
51 const struct gap *rp1 = __ibm932sb_to_ucs4_idx; \
52 const struct gap *rp2 = __ibm932db_to_ucs4_idx; \
53 uint32_t ch = *inptr; \
54 uint32_t res; \
56 if (__builtin_expect (ch, 0) >= 0xffff) \
57 { \
58 rp1 = NULL; \
59 rp2 = NULL; \
60 } \
61 else if (__builtin_expect (ch, 0) == 0x80 \
62 || __builtin_expect (ch, 0) == 0xa0 \
63 || __builtin_expect (ch, 0) == 0xfd \
64 || __builtin_expect (ch, 0) == 0xfe \
65 || __builtin_expect (ch, 0) == 0xff) \
66 { \
67 /* This is an illegal character. */ \
68 if (! ignore_errors_p ()) \
69 { \
70 result = __GCONV_ILLEGAL_INPUT; \
71 break; \
72 } \
73 } \
74 else \
75 { \
76 while (ch > rp1->end) \
77 ++rp1; \
78 } \
80 /* Use the IBM932 table for single byte. */ \
81 if (__builtin_expect (rp1 == NULL, 0) \
82 || __builtin_expect (ch < rp1->start, 0) \
83 || (res = __ibm932sb_to_ucs4[ch + rp1->idx], \
84 __builtin_expect (res, '\1') == 0 && ch != 0)) \
85 { \
87 /* Use the IBM932 table for double byte. */ \
88 if (__builtin_expect (inptr + 1 >= inend, 0)) \
89 { \
90 /* The second character is not available. \
91 Store the intermediate result. */ \
92 result = __GCONV_INCOMPLETE_INPUT; \
93 break; \
94 } \
96 ch = (ch * 0x100) + inptr[1]; \
97 while (ch > rp2->end) \
98 ++rp2; \
100 if (__builtin_expect (rp2 == NULL, 0) \
101 || __builtin_expect (ch < rp2->start, 0) \
102 || (res = __ibm932db_to_ucs4[ch + rp2->idx], \
103 __builtin_expect (res, '\1') == 0 && ch !=0)) \
105 /* This is an illegal character. */ \
106 if (! ignore_errors_p ()) \
108 result = __GCONV_ILLEGAL_INPUT; \
109 break; \
111 ++*irreversible; \
112 inptr += 2; \
113 continue; \
115 else \
117 put32 (outptr, res); \
118 outptr += 4; \
119 inptr += 2; \
122 else \
124 if (res == 0x1c) \
125 res = 0x1a; \
126 else if (res == 0x7f) \
127 res = 0x1c; \
128 else if (res == 0xa5) \
129 res = 0x5c; \
130 else if (res == 0x203e) \
131 res = 0x7e; \
132 else if (res == 0x1a) \
133 res = 0x7f; \
134 put32 (outptr, res); \
135 outptr += 4; \
136 inptr++; \
139 #define LOOP_NEED_FLAGS
140 #include <iconv/loop.c>
142 /* Next, define the other direction. */
143 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
144 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
145 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
146 #define LOOPFCT TO_LOOP
147 #define BODY \
149 const struct gap *rp = __ucs4_to_ibm932sb_idx; \
150 unsigned char sc; \
151 uint32_t ch = get32 (inptr); \
152 uint16_t found = TRUE; \
153 uint32_t i; \
154 uint32_t low; \
155 uint32_t high; \
156 uint16_t pccode; \
158 if (__builtin_expect (ch, 0) >= 0xffff) \
160 UNICODE_TAG_HANDLER (ch, 4); \
161 rp = NULL; \
163 else \
164 while (ch > rp->end) \
165 ++rp; \
167 /* Use the UCS4 table for single byte. */ \
168 if (__builtin_expect (rp == NULL, 0) \
169 || __builtin_expect (ch < rp->start, 0) \
170 || (sc = __ucs4_to_ibm932sb[ch + rp->idx], \
171 __builtin_expect (sc, '\1') == '\0' && ch != L'\0')) \
174 /* Use the UCS4 table for double byte. */ \
175 found = FALSE; \
176 low = 0; \
177 high = (sizeof (__ucs4_to_ibm932db) >> 1) \
178 / sizeof (__ucs4_to_ibm932db[0][FROM]); \
179 pccode = ch; \
180 while (low <= high) \
182 i = (low + high) >> 1; \
183 if (pccode < __ucs4_to_ibm932db[i][FROM]) \
184 high = i - 1; \
185 else if (pccode > __ucs4_to_ibm932db[i][FROM]) \
186 low = i + 1; \
187 else \
189 pccode = __ucs4_to_ibm932db[i][TO]; \
190 found = TRUE; \
191 break; \
194 if (found) \
196 if (__builtin_expect (outptr + 2 > outend, 0)) \
198 result = __GCONV_FULL_OUTPUT; \
199 break; \
201 *outptr++ = pccode >> 8 & 0xff; \
202 *outptr++ = pccode & 0xff; \
204 else \
206 /* This is an illegal character. */ \
207 if (! ignore_errors_p ()) \
209 result = __GCONV_ILLEGAL_INPUT; \
210 break; \
212 ++*irreversible; \
215 else \
217 if (__builtin_expect (outptr + 1 > outend, 0)) \
219 result = __GCONV_FULL_OUTPUT; \
220 break; \
222 if (ch == 0x5c) \
223 *outptr++ = 0x5c; \
224 else if (ch == 0x7e) \
225 *outptr++ = 0x7e; \
226 else \
227 *outptr++ = sc; \
230 /* Now that we wrote the output increment the input pointer. */ \
231 inptr += 4; \
233 #define LOOP_NEED_FLAGS
234 #include <iconv/loop.c>
236 /* Now define the toplevel functions. */
237 #include <iconv/skeleton.c>