]>
Commit | Line | Data |
---|---|---|
2b474353 | 1 | /* Access functions for CNS 11643, plane 1 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> | |
8619129f | 20 | #include <gconv.h> |
2b474353 UD |
21 | |
22 | /* Table for CNS 11643, plane 1 to UCS4 conversion. */ | |
8619129f | 23 | extern const uint16_t __cns11643l1_to_ucs4_tab[]; |
2b474353 UD |
24 | |
25 | ||
9b26f5c4 | 26 | static inline uint32_t |
dd9423a6 | 27 | __attribute ((always_inline)) |
15a2315c UD |
28 | cns11643l1_to_ucs4 (const unsigned char **s, size_t avail, |
29 | unsigned char offset) | |
2b474353 UD |
30 | { |
31 | unsigned char ch = *(*s); | |
32 | unsigned char ch2; | |
33 | int idx; | |
34 | ||
35 | if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x7d) | |
d64b6ad0 | 36 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
37 | |
38 | if (avail < 2) | |
39 | return 0; | |
40 | ||
41 | ch2 = (*s)[1]; | |
42 | if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f) | |
d64b6ad0 | 43 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
44 | |
45 | idx = (ch - 0x21 - offset) * 94 + (ch2 - 0x21 - offset); | |
b79f74cd | 46 | if (idx > 0x21f2) |
d64b6ad0 | 47 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
48 | |
49 | (*s) += 2; | |
50 | ||
d64b6ad0 | 51 | return __cns11643l1_to_ucs4_tab[idx] ?: ((*s) -= 2, __UNKNOWN_10646_CHAR); |
2b474353 UD |
52 | } |
53 | ||
54 | ||
55 | /* Tables for the UCS4 -> CNS conversion. */ | |
8619129f UD |
56 | extern const char __cns11643l1_from_ucs4_tab1[][2]; |
57 | extern const char __cns11643l1_from_ucs4_tab2[][2]; | |
58 | extern const char __cns11643l1_from_ucs4_tab3[][2]; | |
59 | extern const char __cns11643l1_from_ucs4_tab4[][2]; | |
60 | extern const char __cns11643l1_from_ucs4_tab5[][2]; | |
61 | extern const char __cns11643l1_from_ucs4_tab6[][2]; | |
62 | extern const char __cns11643l1_from_ucs4_tab7[][2]; | |
63 | extern const char __cns11643l1_from_ucs4_tab8[][2]; | |
64 | extern const char __cns11643l1_from_ucs4_tab9[][2]; | |
65 | extern const char __cns11643l1_from_ucs4_tab10[][2]; | |
66 | extern const char __cns11643l1_from_ucs4_tab11[][2]; | |
67 | extern const char __cns11643l1_from_ucs4_tab12[][2]; | |
68 | extern const char __cns11643l1_from_ucs4_tab13[][2]; | |
69 | extern const char __cns11643l1_from_ucs4_tab14[][2]; | |
2b474353 UD |
70 | |
71 | ||
72 | static inline size_t | |
dd9423a6 | 73 | __attribute ((always_inline)) |
15a2315c | 74 | ucs4_to_cns11643l1 (uint32_t wch, unsigned char *s, size_t avail) |
2b474353 UD |
75 | { |
76 | unsigned int ch = (unsigned int) wch; | |
77 | char buf[2]; | |
8619129f UD |
78 | const char *cp = buf; |
79 | ||
80 | switch (ch) | |
2b474353 | 81 | { |
8619129f UD |
82 | case 0xa7 ... 0xf7: |
83 | cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7]; | |
84 | break; | |
85 | case 0x2c7 ... 0x2d9: | |
86 | cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7]; | |
87 | break; | |
88 | case 0x391 ... 0x3c9: | |
89 | cp = __cns11643l1_from_ucs4_tab3[ch - 0x391]; | |
9b26f5c4 | 90 | break; |
8619129f UD |
91 | case 0x2013 ... 0x203e: |
92 | cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013]; | |
9b26f5c4 | 93 | break; |
8619129f UD |
94 | case 0x2103: |
95 | cp = "\x22\x6a"; | |
96 | break; | |
97 | case 0x2105: | |
98 | cp = "\x22\x22"; | |
99 | break; | |
100 | case 0x2109: | |
101 | cp = "\x22\x6b"; | |
102 | break; | |
103 | case 0x2160 ... 0x2169: | |
2b474353 UD |
104 | buf[0] = '\x24'; |
105 | buf[1] = '\x2b' + (ch - 0x2160); | |
8619129f UD |
106 | break; |
107 | case 0x2170 ... 0x2179: | |
2b474353 UD |
108 | buf[0] = '\x26'; |
109 | buf[1] = '\x35' + (ch - 0x2170); | |
8619129f | 110 | break; |
9b26f5c4 | 111 | case 0x2190 ... 0x2199: |
8619129f UD |
112 | cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190]; |
113 | break; | |
114 | case 0x2215 ... 0x2267: | |
115 | cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215]; | |
116 | break; | |
117 | case 0x22a5: | |
118 | cp = "\x22\x47"; | |
119 | break; | |
120 | case 0x22bf: | |
121 | cp = "\x22\x4a"; | |
122 | break; | |
123 | case 0x2400 ... 0x2421: | |
124 | cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400]; | |
125 | break; | |
126 | case 0x2460 ... 0x247d: | |
127 | cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460]; | |
128 | break; | |
129 | case 0x2500 ... 0x2642: | |
130 | cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500]; | |
131 | break; | |
132 | case 0x3000 ... 0x3029: | |
133 | cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000]; | |
134 | break; | |
135 | case 0x30fb: | |
136 | cp = "\x21\x26"; | |
137 | break; | |
138 | case 0x3105 ... 0x3129: | |
2b474353 | 139 | buf[0] = '\x25'; |
b79f74cd | 140 | buf[1] = '\x47' + (ch - 0x3105); |
8619129f UD |
141 | break; |
142 | case 0x32a3: | |
143 | cp = "\x22\x21"; | |
144 | break; | |
145 | case 0x338e ... 0x33d5: | |
146 | cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e]; | |
147 | break; | |
148 | case 0x4e00 ... 0x9f9c: | |
149 | cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00]; | |
150 | break; | |
151 | case 0xfe30 ... 0xfe6b: | |
152 | cp = __cns11643l1_from_ucs4_tab13[ch - 0xfe30]; | |
153 | break; | |
154 | case 0xff01 ... 0xff5d: | |
155 | cp = __cns11643l1_from_ucs4_tab14[ch - 0xff01]; | |
156 | break; | |
157 | case 0xffe0: | |
158 | cp = "\x22\x66"; | |
159 | break; | |
160 | case 0xffe1: | |
161 | cp = "\x22\x67"; | |
162 | break; | |
163 | case 0xffe5: | |
164 | cp = "\x22\x64"; | |
165 | break; | |
166 | default: | |
d64b6ad0 | 167 | return __UNKNOWN_10646_CHAR; |
2b474353 | 168 | } |
2b474353 UD |
169 | |
170 | if (cp[0] == '\0') | |
d64b6ad0 | 171 | return __UNKNOWN_10646_CHAR; |
2b474353 UD |
172 | |
173 | if (avail < 2) | |
174 | return 0; | |
175 | ||
176 | s[0] = cp[0]; | |
177 | s[1] = cp[1]; | |
178 | ||
179 | return 2; | |
180 | } |