Update.
[glibc.git] / iconvdata / cns11643.h
blobb57aa9decbfe383c03c3728a5bbfd5ee695cb621
1 /* Access functions for CNS 11643, plane 2 handling.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <stdint.h>
23 /* Table for CNS 11643, plane 2 to UCS4 conversion. */
24 extern const uint16_t __cns11643l2_to_ucs4_tab[];
25 extern const uint16_t __cns11643l14_to_ucs4_tab[];
28 static inline uint32_t
29 cns11643_to_ucs4 (const char **s, size_t avail, unsigned char offset)
31 unsigned char ch = *(*s);
32 unsigned char ch2;
33 unsigned char ch3;
34 uint32_t result;
35 int idx;
37 if (ch < offset || (ch - offset) <= 0x20 || (ch - offset) > 0x30)
38 return __UNKNOWN_10646_CHAR;
40 if (avail < 3)
41 return 0;
43 ch2 = (*s)[1];
44 if ((ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
45 return __UNKNOWN_10646_CHAR;
47 ch3 = (*s)[2];
48 if ((ch3 - offset) <= 0x20 || (ch3 - offset) >= 0x7f)
49 return __UNKNOWN_10646_CHAR;
51 idx = (ch2 - 0x21 - offset) * 94 + (ch3 - 0x21 - offset);
53 if ((ch - 0x20 - offset) == 1)
55 if (idx > 0x21f2)
56 return __UNKNOWN_10646_CHAR;
57 result = __cns11643l1_to_ucs4_tab[idx];
59 else if ((ch - 0x20 - offset) == 2)
61 if (idx > 0x1de1)
62 return __UNKNOWN_10646_CHAR;
63 result = __cns11643l2_to_ucs4_tab[idx];
65 else if ((ch - 0x20 - offset) == 0xe)
67 if (idx > 0x19bd)
68 return __UNKNOWN_10646_CHAR;
69 result = __cns11643l14_to_ucs4_tab[idx];
71 else
72 return __UNKNOWN_10646_CHAR;
74 if (result != L'\0')
75 (*s) += 3;
76 else
77 result = __UNKNOWN_10646_CHAR;
79 return result;
83 /* Tables for the UCS4 -> CNS conversion. */
84 extern const char __cns11643l1_from_ucs4_tab1[][2];
85 extern const char __cns11643l1_from_ucs4_tab2[][2];
86 extern const char __cns11643l1_from_ucs4_tab3[][2];
87 extern const char __cns11643l1_from_ucs4_tab4[][2];
88 extern const char __cns11643l1_from_ucs4_tab5[][2];
89 extern const char __cns11643l1_from_ucs4_tab6[][2];
90 extern const char __cns11643l1_from_ucs4_tab7[][2];
91 extern const char __cns11643l1_from_ucs4_tab8[][2];
92 extern const char __cns11643l1_from_ucs4_tab9[][2];
93 extern const char __cns11643l1_from_ucs4_tab10[][2];
94 extern const char __cns11643l1_from_ucs4_tab11[][2];
95 extern const char __cns11643l1_from_ucs4_tab12[][2];
96 extern const char __cns11643l1_from_ucs4_tab13[][2];
97 extern const char __cns11643l1_from_ucs4_tab14[][2];
98 extern const char __cns11643_from_ucs4_tab[][3];
101 static inline size_t
102 ucs4_to_cns11643 (uint32_t wch, char *s, size_t avail)
104 unsigned int ch = (unsigned int) wch;
105 char buf[2];
106 const char *cp = buf;
107 int needed = 2;
109 switch (ch)
111 case 0xa7 ... 0xf7:
112 cp = __cns11643l1_from_ucs4_tab1[ch - 0xa7];
113 break;
114 case 0x2c7 ... 0x2d9:
115 cp = __cns11643l1_from_ucs4_tab2[ch - 0x2c7];
116 break;
117 case 0x391 ... 0x3c9:
118 cp = __cns11643l1_from_ucs4_tab3[ch - 0x391];
119 break;
120 case 0x2013 ... 0x203e:
121 cp = __cns11643l1_from_ucs4_tab4[ch - 0x2013];
122 break;
123 case 0x2103:
124 cp = "\x22\x6a";
125 break;
126 case 0x2105:
127 cp = "\x22\x22";
128 break;
129 case 0x2109:
130 cp = "\x22\x6b";
131 break;
132 case 0x2160 ... 0x2169:
133 buf[0] = '\x24';
134 buf[1] = '\x2b' + (ch - 0x2160);
135 break;
136 case 0x2170 ... 0x2179:
137 buf[0] = '\x26';
138 buf[1] = '\x35' + (ch - 0x2170);
139 break;
140 case 0x2190 ... 0x2199:
141 cp = __cns11643l1_from_ucs4_tab5[ch - 0x2190];
142 break;
143 case 0x2215 ... 0x2267:
144 cp = __cns11643l1_from_ucs4_tab6[ch - 0x2215];
145 break;
146 case 0x22a5:
147 cp = "\x22\x47";
148 break;
149 case 0x22bf:
150 cp = "\x22\x4a";
151 break;
152 case 0x2400 ... 0x2421:
153 cp = __cns11643l1_from_ucs4_tab7[ch - 0x2400];
154 break;
155 case 0x2460 ... 0x247d:
156 cp = __cns11643l1_from_ucs4_tab8[ch - 0x2460];
157 break;
158 case 0x2500 ... 0x2642:
159 cp = __cns11643l1_from_ucs4_tab9[ch - 0x2500];
160 break;
161 case 0x3000 ... 0x3029:
162 cp = __cns11643l1_from_ucs4_tab10[ch - 0x3000];
163 break;
164 case 0x30fb:
165 cp = "\x21\x26";
166 break;
167 case 0x3105 ... 0x3129:
168 buf[0] = '\x25';
169 buf[1] = '\x47' + (ch - 0x3105);
170 break;
171 case 0x32a3:
172 cp = "\x22\x21";
173 break;
174 case 0x338e ... 0x33d5:
175 cp = __cns11643l1_from_ucs4_tab11[ch - 0x338e];
176 break;
177 case 0x4e00 ... 0x9f9c:
178 cp = __cns11643l1_from_ucs4_tab12[ch - 0x4e00];
180 if (cp[0] != '\0')
181 break;
182 /* FALLTHROUGH. Let's try the other planes. */
183 case 0x9f9d ... 0x9fa5:
184 /* Let's try the other planes. */
185 needed = 3;
186 cp = __cns11643_from_ucs4_tab[ch - 0x4e00];
187 break;
188 case 0xfe30 ... 0xfe6b:
189 cp = __cns11643l1_from_ucs4_tab13[ch - 0xfe30];
190 break;
191 case 0xff01 ... 0xff5d:
192 cp = __cns11643l1_from_ucs4_tab14[ch - 0xff01];
193 break;
194 case 0xffe0:
195 cp = "\x22\x66";
196 break;
197 case 0xffe1:
198 cp = "\x22\x67";
199 break;
200 case 0xffe5:
201 cp = "\x22\x64";
202 break;
203 default:
204 return __UNKNOWN_10646_CHAR;
207 if (cp[0] == '\0')
208 return __UNKNOWN_10646_CHAR;
210 if (avail < needed)
211 return 0;
213 s[0] = cp[0];
214 s[1] = cp[1];
215 if (needed == 3)
216 s[2] = cp[2];
218 return needed;