1 | /* Access functions for CNS 11643 handling. |
2 | Copyright (C) 1998-2021 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. |
5 | |
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. |
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 |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include <stdint.h> |
21 | |
22 | |
23 | /* Table for CNS 11643, plane 1 to UCS4 conversion. */ |
24 | extern const uint16_t __cns11643l1_to_ucs4_tab[]; |
25 | /* Table for CNS 11643, plane 2 to UCS4 conversion. */ |
26 | extern const uint16_t __cns11643l2_to_ucs4_tab[]; |
27 | /* Table for CNS 11643, plane 3 to UCS4 conversion. */ |
28 | extern const uint32_t __cns11643l3_to_ucs4_tab[]; |
29 | /* Table for CNS 11643, plane 4 to UCS4 conversion. */ |
30 | extern const uint32_t __cns11643l4_to_ucs4_tab[]; |
31 | /* Table for CNS 11643, plane 5 to UCS4 conversion. */ |
32 | extern const uint32_t __cns11643l5_to_ucs4_tab[]; |
33 | /* Table for CNS 11643, plane 6 to UCS4 conversion. */ |
34 | extern const uint32_t __cns11643l6_to_ucs4_tab[]; |
35 | /* Table for CNS 11643, plane 7 to UCS4 conversion. */ |
36 | extern const uint32_t __cns11643l7_to_ucs4_tab[]; |
37 | /* Table for CNS 11643, plane 15 (old) to UCS4 conversion. */ |
38 | extern const uint32_t __cns11643l15_to_ucs4_tab[]; |
39 | |
40 | |
41 | static inline uint32_t |
42 | __attribute ((always_inline)) |
43 | cns11643_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset) |
44 | { |
45 | unsigned char ch = *(*s); |
46 | unsigned char ch2; |
47 | unsigned char ch3; |
48 | uint32_t result; |
49 | int idx; |
50 | |
51 | if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x30) |
52 | return __UNKNOWN_10646_CHAR; |
53 | |
54 | if (avail < 3) |
55 | return 0; |
56 | |
57 | ch2 = (*s)[1]; |
58 | if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) |
59 | return __UNKNOWN_10646_CHAR; |
60 | |
61 | ch3 = (*s)[2]; |
62 | if ((ch3 - offset) <= 0x20 || (ch3 - offset) >= 0x7f) |
63 | return __UNKNOWN_10646_CHAR; |
64 | |
65 | idx = (ch2 - 0x21 - offset) * 94 + (ch3 - 0x21 - offset); |
66 | |
67 | switch (ch - 0x20 - offset) |
68 | { |
69 | case 1: |
70 | if (idx > 0x21f2) |
71 | return __UNKNOWN_10646_CHAR; |
72 | result = __cns11643l1_to_ucs4_tab[idx]; |
73 | break; |
74 | case 2: |
75 | if (idx > 0x1de1) |
76 | return __UNKNOWN_10646_CHAR; |
77 | result = __cns11643l2_to_ucs4_tab[idx]; |
78 | break; |
79 | case 3: |
80 | if (idx > 0x19bd) |
81 | return __UNKNOWN_10646_CHAR; |
82 | result = __cns11643l3_to_ucs4_tab[idx]; |
83 | break; |
84 | case 4: |
85 | if (idx > 0x1c81) |
86 | return __UNKNOWN_10646_CHAR; |
87 | result = __cns11643l4_to_ucs4_tab[idx]; |
88 | break; |
89 | case 5: |
90 | if (idx > 0x219a) |
91 | return __UNKNOWN_10646_CHAR; |
92 | result = __cns11643l5_to_ucs4_tab[idx]; |
93 | break; |
94 | case 6: |
95 | if (idx > 0x18f3) |
96 | return __UNKNOWN_10646_CHAR; |
97 | result = __cns11643l6_to_ucs4_tab[idx]; |
98 | break; |
99 | case 7: |
100 | if (idx > 0x198a) |
101 | return __UNKNOWN_10646_CHAR; |
102 | result = __cns11643l7_to_ucs4_tab[idx]; |
103 | break; |
104 | case 15: |
105 | if (idx > 0x1c00) |
106 | return __UNKNOWN_10646_CHAR; |
107 | result = __cns11643l15_to_ucs4_tab[idx]; |
108 | break; |
109 | default: |
110 | return __UNKNOWN_10646_CHAR; |
111 | } |
112 | |
113 | if (result != L'\0') |
114 | (*s) += 3; |
115 | else |
116 | result = __UNKNOWN_10646_CHAR; |
117 | |
118 | return result; |
119 | } |
120 | |
121 | |
122 | /* Tables for the UCS4 -> CNS conversion. */ |
123 | extern const char __cns11643l1_from_ucs4_tab1[][2]; |
124 | extern const char __cns11643l1_from_ucs4_tab2[][2]; |
125 | extern const char __cns11643l1_from_ucs4_tab3[][2]; |
126 | extern const char __cns11643l1_from_ucs4_tab4[][2]; |
127 | extern const char __cns11643l1_from_ucs4_tab5[][2]; |
128 | extern const char __cns11643l1_from_ucs4_tab6[][2]; |
129 | extern const char __cns11643l1_from_ucs4_tab7[][2]; |
130 | extern const char __cns11643l1_from_ucs4_tab8[][2]; |
131 | extern const char __cns11643l1_from_ucs4_tab9[][2]; |
132 | extern const char __cns11643l1_from_ucs4_tab10[][2]; |
133 | extern const char __cns11643l1_from_ucs4_tab11[][2]; |
134 | extern const char __cns11643l1_from_ucs4_tab12[][2]; |
135 | extern const char __cns11643l1_from_ucs4_tab13[][2]; |
136 | extern const char __cns11643l1_from_ucs4_tab14[][2]; |
137 | extern const char __cns11643_from_ucs4p0_tab[][3]; |
138 | extern const char __cns11643_from_ucs4p2_tab[][3]; |
139 | extern const char __cns11643_from_ucs4p2c_tab[][3]; |
140 | |
141 | |
142 | static inline size_t |
143 | __attribute ((always_inline)) |
144 | ucs4_to_cns11643 (uint32_t wch, unsigned char *s, size_t avail) |
145 | { |
146 | unsigned int ch = (unsigned int) wch; |
147 | char buf[2]; |
148 | const char *cp = buf; |
149 | size_t needed = 2; |
150 | |
151 | switch (ch) |
152 | { |
153 | case 0xa7 ... 0xf7: |
154 | cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7]; |
155 | break; |
156 | case 0x2c7 ... 0x2d9: |
157 | cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7]; |
158 | break; |
159 | case 0x391 ... 0x3c9: |
160 | cp = __cns11643l1_from_ucs4_tab3[ch - 0x391]; |
161 | break; |
162 | case 0x2013 ... 0x203e: |
163 | cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013]; |
164 | break; |
165 | case 0x2103: |
166 | cp = "\x22\x6a" ; |
167 | break; |
168 | case 0x2105: |
169 | cp = "\x22\x22" ; |
170 | break; |
171 | case 0x2109: |
172 | cp = "\x22\x6b" ; |
173 | break; |
174 | case 0x2160 ... 0x2169: |
175 | buf[0] = '\x24'; |
176 | buf[1] = '\x2b' + (ch - 0x2160); |
177 | break; |
178 | case 0x2170 ... 0x2179: |
179 | buf[0] = '\x26'; |
180 | buf[1] = '\x35' + (ch - 0x2170); |
181 | break; |
182 | case 0x2190 ... 0x2199: |
183 | cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190]; |
184 | break; |
185 | case 0x2215 ... 0x2267: |
186 | cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215]; |
187 | break; |
188 | case 0x22a5: |
189 | cp = "\x22\x47" ; |
190 | break; |
191 | case 0x22bf: |
192 | cp = "\x22\x4a" ; |
193 | break; |
194 | case 0x2400 ... 0x2421: |
195 | cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400]; |
196 | break; |
197 | case 0x2460 ... 0x247d: |
198 | cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460]; |
199 | break; |
200 | case 0x2500 ... 0x2642: |
201 | cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500]; |
202 | break; |
203 | case 0x3000 ... 0x3029: |
204 | cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000]; |
205 | break; |
206 | case 0x30fb: |
207 | cp = "\x21\x26" ; |
208 | break; |
209 | case 0x3105 ... 0x3129: |
210 | buf[0] = '\x25'; |
211 | buf[1] = '\x47' + (ch - 0x3105); |
212 | break; |
213 | case 0x32a3: |
214 | cp = "\x22\x21" ; |
215 | break; |
216 | case 0x338e ... 0x33d5: |
217 | cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e]; |
218 | break; |
219 | case 0x4e00 ... 0x9f9c: |
220 | cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00]; |
221 | if (cp[0] != '\0') |
222 | break; |
223 | /* Let's try the other planes. */ |
224 | /* Fall through. */ |
225 | case 0x3400 ... 0x4dff: |
226 | case 0x9f9d ... 0x9fa5: |
227 | /* Let's try the other planes. */ |
228 | needed = 3; |
229 | cp = __cns11643_from_ucs4p0_tab[ch - 0x3400]; |
230 | break; |
231 | case 0xfa28: |
232 | needed = 3; |
233 | cp = "\x0f\x58\x4c" ; |
234 | break; |
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; |
250 | case 0x20000 ... 0x2a6d6: |
251 | needed = 3; |
252 | cp = __cns11643_from_ucs4p2_tab[ch - 0x20000]; |
253 | break; |
254 | case 0x2f800 ... 0x2fa1d: |
255 | needed = 3; |
256 | cp = __cns11643_from_ucs4p2c_tab[ch - 0x2f800]; |
257 | break; |
258 | default: |
259 | return __UNKNOWN_10646_CHAR; |
260 | } |
261 | |
262 | if (cp[0] == '\0') |
263 | return __UNKNOWN_10646_CHAR; |
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 | } |
275 | |