* sysdeps/ieee754/ldbl-opt/Makefile (sysdep-CFLAGS): Avoid adding
[glibc.git] / iconvdata / ibm932.c
blob8f05581c13ec54690ff13a2abe8d917d88affdc9
1 /* Conversion from and to IBM932.
2 Copyright (C) 2000-2002, 2004 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 <dlfcn.h>
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "ibm932.h"
26 #define FROM 0
27 #define TO 1
29 /* Definitions used in the body of the `gconv' function. */
30 #define CHARSET_NAME "IBM932//"
31 #define FROM_LOOP from_ibm932
32 #define TO_LOOP to_ibm932
34 /* Definitions of initialization and destructor function. */
35 #define DEFINE_INIT 1
36 #define DEFINE_FINI 1
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
47 #define BODY \
48 { \
49 const struct gap *rp2 = __ibm932db_to_ucs4_idx; \
50 uint32_t ch = *inptr; \
51 uint32_t res; \
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)) \
58 { \
59 /* This is an illegal character. */ \
60 STANDARD_FROM_LOOP_ERR_HANDLER (1); \
61 } \
63 /* Use the IBM932 table for single byte. */ \
64 res = __ibm932sb_to_ucs4[ch]; \
65 if (__builtin_expect (res == 0, 0) && ch != 0) \
66 { \
67 /* Use the IBM932 table for double byte. */ \
68 if (__builtin_expect (inptr + 1 >= inend, 0)) \
69 { \
70 /* The second character is not available. \
71 Store the intermediate result. */ \
72 result = __GCONV_INCOMPLETE_INPUT; \
73 break; \
74 } \
76 ch = (ch * 0x100) + inptr[1]; \
77 while (ch > rp2->end) \
78 ++rp2; \
80 if (__builtin_expect (rp2 == NULL, 0) \
81 || __builtin_expect (ch < rp2->start, 0) \
82 || (res = __ibm932db_to_ucs4[ch + rp2->idx], \
83 __builtin_expect (res, '\1') == 0 && ch !=0)) \
84 { \
85 /* This is an illegal character. */ \
86 STANDARD_FROM_LOOP_ERR_HANDLER (2); \
87 } \
88 else \
89 { \
90 put32 (outptr, res); \
91 outptr += 4; \
92 inptr += 2; \
93 } \
94 } \
95 else \
96 { \
97 if (res == 0x1c) \
98 res = 0x1a; \
99 else if (res == 0x7f) \
100 res = 0x1c; \
101 else if (res == 0xa5) \
102 res = 0x5c; \
103 else if (res == 0x203e) \
104 res = 0x7e; \
105 else if (res == 0x1a) \
106 res = 0x7f; \
107 put32 (outptr, res); \
108 outptr += 4; \
109 inptr++; \
112 #define LOOP_NEED_FLAGS
113 #define ONEBYTE_BODY \
115 if (c == 0x80 || c == 0xa0 || c >= 0xfd) \
116 return WEOF; \
117 uint32_t res = __ibm932sb_to_ucs4[c]; \
118 if (res == 0 && c != 0) \
119 return WEOF; \
120 if (res == 0x1c) \
121 res = 0x1a; \
122 else if (res == 0x7f) \
123 res = 0x1c; \
124 else if (res == 0xa5) \
125 res = 0x5c; \
126 else if (res == 0x203e) \
127 res = 0x7e; \
128 else if (res == 0x1a) \
129 res = 0x7f; \
130 return res; \
132 #include <iconv/loop.c>
134 /* Next, define the other direction. */
135 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
136 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
137 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
138 #define LOOPFCT TO_LOOP
139 #define BODY \
141 const struct gap *rp = __ucs4_to_ibm932sb_idx; \
142 unsigned char sc; \
143 uint32_t ch = get32 (inptr); \
144 bool found = true; \
145 uint32_t i; \
146 uint32_t low; \
147 uint32_t high; \
148 uint16_t pccode; \
150 if (__builtin_expect (ch >= 0xffff, 0)) \
152 UNICODE_TAG_HANDLER (ch, 4); \
153 rp = NULL; \
155 else \
156 while (ch > rp->end) \
157 ++rp; \
159 /* Use the UCS4 table for single byte. */ \
160 if (__builtin_expect (rp == NULL, 0) \
161 || __builtin_expect (ch < rp->start, 0) \
162 || (sc = __ucs4_to_ibm932sb[ch + rp->idx], \
163 __builtin_expect (sc, '\1') == '\0' && ch != L'\0')) \
166 /* Use the UCS4 table for double byte. */ \
167 found = false; \
168 low = 0; \
169 high = (sizeof (__ucs4_to_ibm932db) >> 1) \
170 / sizeof (__ucs4_to_ibm932db[0][FROM]); \
171 pccode = ch; \
172 if (__builtin_expect (rp != NULL, 1)) \
173 while (low < high) \
175 i = (low + high) >> 1; \
176 if (pccode < __ucs4_to_ibm932db[i][FROM]) \
177 high = i; \
178 else if (pccode > __ucs4_to_ibm932db[i][FROM]) \
179 low = i + 1; \
180 else \
182 pccode = __ucs4_to_ibm932db[i][TO]; \
183 found = true; \
184 break; \
187 if (found) \
189 if (__builtin_expect (outptr + 2 > outend, 0)) \
191 result = __GCONV_FULL_OUTPUT; \
192 break; \
194 *outptr++ = pccode >> 8 & 0xff; \
195 *outptr++ = pccode & 0xff; \
197 else \
199 /* This is an illegal character. */ \
200 STANDARD_TO_LOOP_ERR_HANDLER (4); \
203 else \
205 if (__builtin_expect (outptr + 1 > outend, 0)) \
207 result = __GCONV_FULL_OUTPUT; \
208 break; \
210 if (ch == 0x5c) \
211 *outptr++ = 0x5c; \
212 else if (ch == 0x7e) \
213 *outptr++ = 0x7e; \
214 else \
215 *outptr++ = sc; \
218 /* Now that we wrote the output increment the input pointer. */ \
219 inptr += 4; \
221 #define LOOP_NEED_FLAGS
222 #include <iconv/loop.c>
224 /* Now define the toplevel functions. */
225 #include <iconv/skeleton.c>