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