]>
Commit | Line | Data |
---|---|---|
289ac9dd | 1 | /* Access functions for CNS 11643 handling. |
085a4412 UD |
2 | Copyright (C) 1998,1999,2000,2001,2002,2003,2007 |
3 | Free Software Foundation, Inc. | |
2b474353 UD |
4 | This file is part of the GNU C Library. |
5 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. | |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
2b474353 UD |
11 | |
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
2b474353 | 16 | |
41bdb6e2 | 17 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
18 | License along with the GNU C Library; if not, see |
19 | <http://www.gnu.org/licenses/>. */ | |
2b474353 UD |
20 | |
21 | #include <stdint.h> | |
22 | ||
289ac9dd | 23 | |
755104ed UD |
24 | /* Table for CNS 11643, plane 1 to UCS4 conversion. */ |
25 | extern const uint16_t __cns11643l1_to_ucs4_tab[]; | |
2b474353 | 26 | /* Table for CNS 11643, plane 2 to UCS4 conversion. */ |
8619129f | 27 | extern const uint16_t __cns11643l2_to_ucs4_tab[]; |
289ac9dd UD |
28 | /* Table for CNS 11643, plane 3 to UCS4 conversion. */ |
29 | extern const uint32_t __cns11643l3_to_ucs4_tab[]; | |
30 | /* Table for CNS 11643, plane 4 to UCS4 conversion. */ | |
31 | extern const uint32_t __cns11643l4_to_ucs4_tab[]; | |
32 | /* Table for CNS 11643, plane 5 to UCS4 conversion. */ | |
33 | extern const uint32_t __cns11643l5_to_ucs4_tab[]; | |
34 | /* Table for CNS 11643, plane 6 to UCS4 conversion. */ | |
35 | extern const uint32_t __cns11643l6_to_ucs4_tab[]; | |
36 | /* Table for CNS 11643, plane 7 to UCS4 conversion. */ | |
37 | extern const uint32_t __cns11643l7_to_ucs4_tab[]; | |
38 | /* Table for CNS 11643, plane 15 (old) to UCS4 conversion. */ | |
39 | extern const uint32_t __cns11643l15_to_ucs4_tab[]; | |
2b474353 UD |
40 | |
41 | ||
9b26f5c4 | 42 | static inline uint32_t |
dd9423a6 | 43 | __attribute ((always_inline)) |
085a4412 | 44 | cns11643_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset) |
2b474353 UD |
45 | { |
46 | unsigned char ch = *(*s); | |
47 | unsigned char ch2; | |
48 | unsigned char ch3; | |
9b26f5c4 | 49 | uint32_t result; |
2b474353 UD |
50 | int idx; |
51 | ||
52 | if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x30) | |
d64b6ad0 | 53 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
54 | |
55 | if (avail < 3) | |
56 | return 0; | |
57 | ||
58 | ch2 = (*s)[1]; | |
59 | if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) | |
d64b6ad0 | 60 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
61 | |
62 | ch3 = (*s)[2]; | |
63 | if ((ch3 - offset) <= 0x20 || (ch3 - offset) >= 0x7f) | |
d64b6ad0 | 64 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
65 | |
66 | idx = (ch2 - 0x21 - offset) * 94 + (ch3 - 0x21 - offset); | |
67 | ||
289ac9dd | 68 | switch (ch - 0x20 - offset) |
2b474353 | 69 | { |
289ac9dd | 70 | case 1: |
b79f74cd | 71 | if (idx > 0x21f2) |
d64b6ad0 | 72 | return __UNKNOWN_10646_CHAR; |
8619129f | 73 | result = __cns11643l1_to_ucs4_tab[idx]; |
289ac9dd UD |
74 | break; |
75 | case 2: | |
2b474353 | 76 | if (idx > 0x1de1) |
d64b6ad0 | 77 | return __UNKNOWN_10646_CHAR; |
8619129f | 78 | result = __cns11643l2_to_ucs4_tab[idx]; |
289ac9dd UD |
79 | break; |
80 | case 3: | |
2b474353 | 81 | if (idx > 0x19bd) |
d64b6ad0 | 82 | return __UNKNOWN_10646_CHAR; |
289ac9dd UD |
83 | result = __cns11643l3_to_ucs4_tab[idx]; |
84 | break; | |
85 | case 4: | |
86 | if (idx > 0x1c81) | |
87 | return __UNKNOWN_10646_CHAR; | |
88 | result = __cns11643l4_to_ucs4_tab[idx]; | |
89 | break; | |
90 | case 5: | |
91 | if (idx > 0x219a) | |
92 | return __UNKNOWN_10646_CHAR; | |
93 | result = __cns11643l5_to_ucs4_tab[idx]; | |
94 | break; | |
95 | case 6: | |
96 | if (idx > 0x18f3) | |
97 | return __UNKNOWN_10646_CHAR; | |
98 | result = __cns11643l6_to_ucs4_tab[idx]; | |
99 | break; | |
100 | case 7: | |
101 | if (idx > 0x198a) | |
102 | return __UNKNOWN_10646_CHAR; | |
103 | result = __cns11643l7_to_ucs4_tab[idx]; | |
104 | break; | |
105 | case 15: | |
106 | if (idx > 0x1c00) | |
107 | return __UNKNOWN_10646_CHAR; | |
108 | result = __cns11643l15_to_ucs4_tab[idx]; | |
109 | break; | |
110 | default: | |
111 | return __UNKNOWN_10646_CHAR; | |
2b474353 | 112 | } |
2b474353 UD |
113 | |
114 | if (result != L'\0') | |
115 | (*s) += 3; | |
116 | else | |
d64b6ad0 | 117 | result = __UNKNOWN_10646_CHAR; |
2b474353 UD |
118 | |
119 | return result; | |
120 | } | |
121 | ||
122 | ||
123 | /* Tables for the UCS4 -> CNS conversion. */ | |
8619129f UD |
124 | extern const char __cns11643l1_from_ucs4_tab1[][2]; |
125 | extern const char __cns11643l1_from_ucs4_tab2[][2]; | |
126 | extern const char __cns11643l1_from_ucs4_tab3[][2]; | |
127 | extern const char __cns11643l1_from_ucs4_tab4[][2]; | |
128 | extern const char __cns11643l1_from_ucs4_tab5[][2]; | |
129 | extern const char __cns11643l1_from_ucs4_tab6[][2]; | |
130 | extern const char __cns11643l1_from_ucs4_tab7[][2]; | |
131 | extern const char __cns11643l1_from_ucs4_tab8[][2]; | |
132 | extern const char __cns11643l1_from_ucs4_tab9[][2]; | |
133 | extern const char __cns11643l1_from_ucs4_tab10[][2]; | |
134 | extern const char __cns11643l1_from_ucs4_tab11[][2]; | |
135 | extern const char __cns11643l1_from_ucs4_tab12[][2]; | |
136 | extern const char __cns11643l1_from_ucs4_tab13[][2]; | |
137 | extern const char __cns11643l1_from_ucs4_tab14[][2]; | |
289ac9dd UD |
138 | extern const char __cns11643_from_ucs4p0_tab[][3]; |
139 | extern const char __cns11643_from_ucs4p2_tab[][3]; | |
93a568aa | 140 | extern const char __cns11643_from_ucs4p2c_tab[][3]; |
2b474353 UD |
141 | |
142 | ||
143 | static inline size_t | |
dd9423a6 | 144 | __attribute ((always_inline)) |
085a4412 | 145 | ucs4_to_cns11643 (uint32_t wch, unsigned char *s, size_t avail) |
2b474353 UD |
146 | { |
147 | unsigned int ch = (unsigned int) wch; | |
148 | char buf[2]; | |
8619129f | 149 | const char *cp = buf; |
8c0b7170 | 150 | size_t needed = 2; |
2b474353 | 151 | |
8619129f | 152 | switch (ch) |
2b474353 | 153 | { |
8619129f UD |
154 | case 0xa7 ... 0xf7: |
155 | cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7]; | |
156 | break; | |
157 | case 0x2c7 ... 0x2d9: | |
158 | cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7]; | |
159 | break; | |
160 | case 0x391 ... 0x3c9: | |
161 | cp = __cns11643l1_from_ucs4_tab3[ch - 0x391]; | |
162 | break; | |
163 | case 0x2013 ... 0x203e: | |
164 | cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013]; | |
165 | break; | |
166 | case 0x2103: | |
167 | cp = "\x22\x6a"; | |
168 | break; | |
169 | case 0x2105: | |
170 | cp = "\x22\x22"; | |
171 | break; | |
172 | case 0x2109: | |
173 | cp = "\x22\x6b"; | |
174 | break; | |
9b26f5c4 | 175 | case 0x2160 ... 0x2169: |
2b474353 UD |
176 | buf[0] = '\x24'; |
177 | buf[1] = '\x2b' + (ch - 0x2160); | |
8619129f UD |
178 | break; |
179 | case 0x2170 ... 0x2179: | |
2b474353 UD |
180 | buf[0] = '\x26'; |
181 | buf[1] = '\x35' + (ch - 0x2170); | |
8619129f UD |
182 | break; |
183 | case 0x2190 ... 0x2199: | |
184 | cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190]; | |
185 | break; | |
186 | case 0x2215 ... 0x2267: | |
187 | cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215]; | |
188 | break; | |
189 | case 0x22a5: | |
190 | cp = "\x22\x47"; | |
191 | break; | |
192 | case 0x22bf: | |
193 | cp = "\x22\x4a"; | |
194 | break; | |
195 | case 0x2400 ... 0x2421: | |
196 | cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400]; | |
197 | break; | |
198 | case 0x2460 ... 0x247d: | |
199 | cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460]; | |
200 | break; | |
201 | case 0x2500 ... 0x2642: | |
202 | cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500]; | |
9b26f5c4 | 203 | break; |
8619129f UD |
204 | case 0x3000 ... 0x3029: |
205 | cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000]; | |
206 | break; | |
207 | case 0x30fb: | |
208 | cp = "\x21\x26"; | |
209 | break; | |
210 | case 0x3105 ... 0x3129: | |
2b474353 | 211 | buf[0] = '\x25'; |
b79f74cd | 212 | buf[1] = '\x47' + (ch - 0x3105); |
8619129f UD |
213 | break; |
214 | case 0x32a3: | |
215 | cp = "\x22\x21"; | |
216 | break; | |
217 | case 0x338e ... 0x33d5: | |
218 | cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e]; | |
219 | break; | |
220 | case 0x4e00 ... 0x9f9c: | |
221 | cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00]; | |
b79f74cd UD |
222 | if (cp[0] != '\0') |
223 | break; | |
224 | /* FALLTHROUGH. Let's try the other planes. */ | |
289ac9dd | 225 | case 0x3400 ... 0x4dff: |
b79f74cd UD |
226 | case 0x9f9d ... 0x9fa5: |
227 | /* Let's try the other planes. */ | |
228 | needed = 3; | |
289ac9dd | 229 | cp = __cns11643_from_ucs4p0_tab[ch - 0x3400]; |
8619129f | 230 | break; |
93a568aa UD |
231 | case 0xfa28: |
232 | needed = 3; | |
233 | cp = "\x0f\x58\x4c"; | |
234 | break; | |
8619129f UD |
235 | case 0xfe30 ... 0xfe6b: |
236 | cp = __cns11643l1_from_ucs4_tab13[ch - 0xfe30]; | |
237 | break; | |
238 | case 0xff01 ... 0xff5d: | |
239 | cp = __cns11643l1_from_ucs4_tab14[ch - 0xff01]; | |
240 | break; | |
241 | case 0xffe0: | |
242 | cp = "\x22\x66"; | |
243 | break; | |
244 | case 0xffe1: | |
245 | cp = "\x22\x67"; | |
246 | break; | |
247 | case 0xffe5: | |
248 | cp = "\x22\x64"; | |
249 | break; | |
289ac9dd UD |
250 | case 0x20000 ... 0x2a6d6: |
251 | needed = 3; | |
252 | cp = __cns11643_from_ucs4p2_tab[ch - 0x20000]; | |
253 | break; | |
93a568aa UD |
254 | case 0x2f800 ... 0x2fa1d: |
255 | needed = 3; | |
256 | cp = __cns11643_from_ucs4p2c_tab[ch - 0x2f800]; | |
257 | break; | |
8619129f | 258 | default: |
d64b6ad0 | 259 | return __UNKNOWN_10646_CHAR; |
2b474353 | 260 | } |
2b474353 UD |
261 | |
262 | if (cp[0] == '\0') | |
d64b6ad0 | 263 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
264 | |
265 | if (avail < needed) | |
266 | return 0; | |
267 | ||
268 | s[0] = cp[0]; | |
269 | s[1] = cp[1]; | |
270 | if (needed == 3) | |
271 | s[2] = cp[2]; | |
272 | ||
273 | return needed; | |
274 | } |