]>
Commit | Line | Data |
---|---|---|
3b7caeac | 1 | /* Conversion from and to IBM943. |
568035b7 | 2 | Copyright (C) 2000-2013 Free Software Foundation, Inc. |
3b7caeac UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Masahide Washizawa <washi@jp.ibm.com>, 2000. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
3b7caeac UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
3b7caeac | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
3b7caeac | 19 | |
e438a468 UD |
20 | #include <dlfcn.h> |
21 | #include <stdint.h> | |
f9ad060c | 22 | #include <stdbool.h> |
3b7caeac UD |
23 | #include "ibm943.h" |
24 | ||
3b7caeac | 25 | #define FROM 0 |
db2d05f9 | 26 | #define TO 1 |
3b7caeac UD |
27 | |
28 | /* Definitions used in the body of the `gconv' function. */ | |
29 | #define CHARSET_NAME "IBM943//" | |
30 | #define FROM_LOOP from_ibm943 | |
31 | #define TO_LOOP to_ibm943 | |
32 | ||
33 | /* Definitions of initialization and destructor function. */ | |
34 | #define DEFINE_INIT 1 | |
35 | #define DEFINE_FINI 1 | |
36 | ||
37 | #define MIN_NEEDED_FROM 1 | |
38 | #define MAX_NEEDED_FROM 2 | |
39 | #define MIN_NEEDED_TO 4 | |
40 | ||
41 | /* First, define the conversion function from IBM-943 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 | |
46 | #define BODY \ | |
47 | { \ | |
3b7caeac UD |
48 | const struct gap *rp2 = __ibm943db_to_ucs4_idx; \ |
49 | uint32_t ch = *inptr; \ | |
50 | uint32_t res; \ | |
51 | \ | |
f9ad060c UD |
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)) \ | |
3b7caeac UD |
57 | { \ |
58 | /* This is an illegal character. */ \ | |
e438a468 | 59 | STANDARD_FROM_LOOP_ERR_HANDLER (1); \ |
3b7caeac UD |
60 | } \ |
61 | \ | |
62 | /* Use the IBM943 table for single byte. */ \ | |
f9ad060c UD |
63 | if (__builtin_expect (ch > 0xdf, 0) \ |
64 | || (res = __ibm943sb_to_ucs4[ch], \ | |
65 | __builtin_expect (res == 0, 0) && ch != 0)) \ | |
3b7caeac | 66 | { \ |
3b7caeac UD |
67 | /* Use the IBM943 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 | } \ | |
75 | \ | |
76 | ch = (ch * 0x100) + inptr[1]; \ | |
77 | while (ch > rp2->end) \ | |
78 | ++rp2; \ | |
79 | \ | |
80 | if (__builtin_expect (rp2 == NULL, 0) \ | |
81 | || __builtin_expect (ch < rp2->start, 0) \ | |
82 | || (res = __ibm943db_to_ucs4[ch + rp2->idx], \ | |
83 | __builtin_expect (res, '\1') == 0 && ch !=0)) \ | |
84 | { \ | |
85 | /* This is an illegal character. */ \ | |
e438a468 | 86 | STANDARD_FROM_LOOP_ERR_HANDLER (2); \ |
3b7caeac UD |
87 | } \ |
88 | else \ | |
89 | { \ | |
90 | put32 (outptr, res); \ | |
91 | outptr += 4; \ | |
92 | inptr += 2; \ | |
93 | } \ | |
94 | } \ | |
95 | else \ | |
96 | { \ | |
15627a21 | 97 | if (res == 0xa5) \ |
3b7caeac UD |
98 | res = 0x5c; \ |
99 | else if (res == 0x203e) \ | |
100 | res = 0x7e; \ | |
3b7caeac UD |
101 | put32 (outptr, res); \ |
102 | outptr += 4; \ | |
103 | inptr++; \ | |
104 | } \ | |
105 | } | |
106 | #define LOOP_NEED_FLAGS | |
f9ad060c UD |
107 | #define ONEBYTE_BODY \ |
108 | { \ | |
109 | if (c == 0x80 || c == 0xa0 || c >= 0xe0) \ | |
110 | return WEOF; \ | |
111 | uint32_t res = __ibm943sb_to_ucs4[c]; \ | |
112 | if (res == 0 && c != 0) \ | |
113 | return WEOF; \ | |
15627a21 | 114 | if (res == 0xa5) \ |
f9ad060c UD |
115 | res = 0x5c; \ |
116 | else if (res == 0x203e) \ | |
117 | res = 0x7e; \ | |
f9ad060c UD |
118 | return res; \ |
119 | } | |
3b7caeac UD |
120 | #include <iconv/loop.c> |
121 | ||
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 | |
127 | #define BODY \ | |
128 | { \ | |
129 | const struct gap *rp = __ucs4_to_ibm943sb_idx; \ | |
130 | unsigned char sc; \ | |
131 | uint32_t ch = get32(inptr); \ | |
f9ad060c | 132 | bool found = true; \ |
3b7caeac UD |
133 | uint32_t i; \ |
134 | uint32_t low; \ | |
135 | uint32_t high; \ | |
136 | uint16_t pccode; \ | |
137 | \ | |
db2d05f9 | 138 | if (__builtin_expect (ch >= 0xffff, 0)) \ |
601d2942 UD |
139 | { \ |
140 | UNICODE_TAG_HANDLER (ch, 4); \ | |
3b7caeac | 141 | rp = NULL; \ |
601d2942 | 142 | } \ |
3b7caeac UD |
143 | else \ |
144 | while (ch > rp->end) \ | |
145 | ++rp; \ | |
146 | \ | |
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_ibm943sb[ch + rp->idx], \ | |
151 | __builtin_expect (sc, '\1') == '\0' && ch != L'\0')) \ | |
152 | { \ | |
153 | \ | |
154 | /* Use the UCS4 table for double byte. */ \ | |
f9ad060c | 155 | found = false; \ |
3b7caeac UD |
156 | low = 0; \ |
157 | high = (sizeof (__ucs4_to_ibm943db) >> 1) \ | |
158 | / sizeof (__ucs4_to_ibm943db[0][FROM]); \ | |
159 | pccode = ch; \ | |
5d32be9a UD |
160 | if (__builtin_expect (rp != NULL, 1)) \ |
161 | while (low < high) \ | |
162 | { \ | |
163 | i = (low + high) >> 1; \ | |
164 | if (pccode < __ucs4_to_ibm943db[i][FROM]) \ | |
165 | high = i; \ | |
166 | else if (pccode > __ucs4_to_ibm943db[i][FROM]) \ | |
167 | low = i + 1; \ | |
168 | else \ | |
169 | { \ | |
170 | pccode = __ucs4_to_ibm943db[i][TO]; \ | |
171 | found = true; \ | |
172 | break; \ | |
173 | } \ | |
174 | } \ | |
601d2942 UD |
175 | if (found) \ |
176 | { \ | |
177 | if (__builtin_expect (outptr + 2 > outend, 0)) \ | |
178 | { \ | |
179 | result = __GCONV_FULL_OUTPUT; \ | |
180 | break; \ | |
181 | } \ | |
182 | *outptr++ = pccode >> 8 & 0xff; \ | |
183 | *outptr++ = pccode & 0xff; \ | |
184 | } \ | |
185 | else \ | |
186 | { \ | |
187 | /* This is an illegal character. */ \ | |
e438a468 | 188 | STANDARD_TO_LOOP_ERR_HANDLER (4); \ |
601d2942 | 189 | } \ |
3b7caeac UD |
190 | } \ |
191 | else \ | |
192 | { \ | |
193 | if (__builtin_expect (outptr + 1 > outend, 0)) \ | |
194 | { \ | |
195 | result = __GCONV_FULL_OUTPUT; \ | |
196 | break; \ | |
197 | } \ | |
198 | if (ch == 0x5c) \ | |
199 | *outptr++ = 0x5c; \ | |
200 | else if (ch == 0x7e) \ | |
201 | *outptr++ = 0x7e; \ | |
202 | else \ | |
203 | *outptr++ = sc; \ | |
204 | } \ | |
205 | \ | |
601d2942 UD |
206 | /* Now that we wrote the output increment the input pointer. */ \ |
207 | inptr += 4; \ | |
3b7caeac UD |
208 | } |
209 | #define LOOP_NEED_FLAGS | |
210 | #include <iconv/loop.c> | |
211 | ||
212 | /* Now define the toplevel functions. */ | |
213 | #include <iconv/skeleton.c> |